poj 2395 bfs/记录路径
http://poj.org/problem?id=2935
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 3220 | Accepted: 1457 | Special Judge | ||
Description
In this problem you have to solve a very simple maze consisting of:
- a 6 by 6 grid of unit squares
- 3 walls of length between 1 and 6 which are placed either horizontally or vertically to separate squares
- one start and one end marker
A maze may look like this:

You have to find a shortest path between the square with the start marker and the square with the end marker. Only moves between adjacent grid squares are allowed; adjacent means that the grid squares share an edge and are not separated by a wall. It is not allowed to leave the grid.
Input
The input consists of several test cases. Each test case consists of five lines: The first line contains the column and row number of the square with the start marker, the second line the column and row number of the square with the end marker. The third, fourth and fifth lines specify the locations of the three walls. The location of a wall is specified by either the position of its left end point followed by the position of its right end point (in case of a horizontal wall) or the position of its upper end point followed by the position of its lower end point (in case of a vertical wall). The position of a wall end point is given as the distance from the left side of the grid followed by the distance from the upper side of the grid.
You may assume that the three walls don’t intersect with each other, although they may touch at some grid corner, and that the wall endpoints are on the grid. Moreover, there will always be a valid path from the start marker to the end marker. Note that the sample input specifies the maze from the picture above.
The last test case is followed by a line containing two zeros.
Output
For each test case print a description of a shortest path from the start marker to the end marker. The description should specify the direction of every move (‘N’ for up, ‘E’ for right, ‘S’ for down and ‘W’ for left).
There can be more than one shortest path, in this case you can print any of them.
Sample Input
1 6
2 6
0 0 1 0
1 5 1 6
1 5 3 5
0 0
Sample Output
NEEESWW
Source
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
bool can[][][][];
bool vis[][];
int path[][];
int fx[][]={,,-,,,,,-};
char idx[]={'N','S','W','E'};
struct node{int x,y,bs;}P[];
void print(int x,int y,int bs)
{
if(bs==) return;
for(int i=;i<;++i)
{
int dx=x+fx[i][];
int dy=y+fx[i][];
if(dx<||dy<||dx>||dy>||path[dx][dy]+!=bs||can[dx][dy][x][y]) continue;
else{
print(dx,dy,bs-);
printf("%c",idx[i]);
return;
}
}
}
void bfs(node s,node e)
{
memset(vis,,sizeof(vis));
memset(path,inf,sizeof(path));
path[s.x][s.y]=;
queue<node>q;
s.bs=;
q.push(s);
while(!q.empty()){
node t=q.front();q.pop();
if(t.x==e.x&&t.y==e.y) {print(t.x,t.y,t.bs);return;}
if(vis[t.x][t.y]) continue;
vis[t.x][t.y]=;
for(int i=;i<;++i)
{
node tt=t;
int dx=tt.x+fx[i][];
int dy=tt.y+fx[i][];
if(dx<||dy<||dx>||dy>||vis[dx][dy]||can[tt.x][tt.y][dx][dy]||path[dx][dy]<=tt.bs+) continue;
path[dx][dy]=tt.bs+;
q.push(node{dx,dy,tt.bs+});
}
}
}
int main()
{
while(scanf("%d%d",&P[].y,&P[].x)!=EOF){
if(P[].x==&&P[].y==) break;
scanf("%d%d",&P[].y,&P[].x);
memset(can,,sizeof(can));
for(int i=;i<;++i)
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&y1,&x1,&y2,&x2);
if(x1==x2){
int miny=min(y1,y2)+,maxy=max(y1,y2);
for(int j=miny;j<=maxy;++j)
can[x1][j][x1+][j]=can[x1+][j][x1][j]=;
}
else{
int minx=min(x1,x2)+,maxx=max(x1,x2);
for(int j=minx;j<=maxx;++j)
{
can[j][y1][j][y1+]=can[j][y1+][j][y1]=;
}
}
}
bfs(P[],P[]);
puts("");
}
return ;
}
poj 2395 bfs/记录路径的更多相关文章
- - 迷宫问题 POJ - 3984 bfs记录路径并输出最短路径
定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...
- POJ.3894 迷宫问题 (BFS+记录路径)
POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...
- Codeforces-A. Shortest path of the king(简单bfs记录路径)
A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...
- HDU1026--Ignatius and the Princess I(BFS记录路径)
Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- (简单) POJ 3414 Pots,BFS+记录路径。
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...
- hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)
以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...
- POJ 3414 Pots 记录路径的广搜
Description You are given two pots, having the volume of A and B liters respectively. The following ...
随机推荐
- element-ui中下拉菜单中的@click事件不会触发的问题
只需要将@click=“fun()”改为@click.native=“fun()”,即可监听下拉菜单的点击事件. 如图所示: 嗯,就酱~
- 001-web基本程序搭建
一.IDEA创建项目 1.基本项目创建 1.1.基本步骤 1.Create New Project [File→New→Project]→New Project 2.maven→group.artif ...
- SaltStack任务计划
编辑fansik_cron.sls文件: 内容如下: cron_test: cron.present: - name: /bin/touch /tmp/fansik.txt - user: root ...
- mysql之视图,触发器,事务等
一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的 ...
- 每天一个Linux命令(62)rcp命令
rcp代表"remote file copy"(远程文件拷贝). (1)用法: 用法: rcp [参数] [源文件] [目标文件] (2)功能: ...
- $Java HttpClient库的使用
(一)简介 HttpClient是Apache的一个开源库,相比于JDK自带的URLConnection等,使用起来更灵活方便. 使用方法可以大致分为如下八步曲: 1.创建一个HttpClient对象 ...
- 吐槽 MySQL数据库jdbc操作,varchar类型占位符问题——单引号造孽
很长时间不写代码动手能力明显下降很多常见的错误还是经常发生,今天吐血了一次. 简单的坑总是要多跳几次才能甘心.很清晰的记得大学的时候在此坑差点闷死,现在又跳进这个坑了,搞了半天终于知道错在哪里. St ...
- Linux基础三---打包压缩&vim&系统的初始化和服务
一,常用命令——tar&vim 1. tar [参数] 文件名 [路径] 参数: -c :建立一个压缩文件的参数指令(create 的意思): -x :解开一个压缩文件的参数指令! ...
- Nagios 服务安装
Nagios 环境部署 安装服务包 操作系统:Linux Centos 6.4 32位 安装包:nagios-3.4.3.tar.gz 安装包:nagios-plugins-1.4.13.tar.gz ...
- 纯CSS3动画按钮效果
在线演示 本地下载