http://poj.org/problem?id=2935

Basic Wall Maze
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:

  1. a 6 by 6 grid of unit squares
  2. 3 walls of length between 1 and 6 which are placed either horizontally or vertically to separate squares
  3. 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

  6*6地图给出三堵墙,输出一条最短路径方案。
  我们用path数组,path[i][j]表示从起点到(i,j)时的最短步数,这样每次找到相邻格子中步数相差1的且中间没墙的通过,递归输出一下。唯一的坑点就是输出时注意判断是否有墙,利用自己造的数据发现了所以1A。

 #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/记录路径的更多相关文章

  1. - 迷宫问题 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, ...

  2. POJ.3894 迷宫问题 (BFS+记录路径)

    POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...

  3. 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 ...

  4. HDU1026--Ignatius and the Princess I(BFS记录路径)

    Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...

  5. poj 3414 Pots 【BFS+记录路径 】

    //yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...

  6. (简单) POJ 3414 Pots,BFS+记录路径。

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  7. 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 ...

  8. hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)

    以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...

  9. POJ 3414 Pots 记录路径的广搜

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

随机推荐

  1. win7安装composer

    安装前请务必确保已经正确安装了 PHP.打开命令行窗口并执行 php -v 查看是否正确输出版本号. 开始安装前需要把open_ssl扩展打开 打开命令行并依次执行下列命令安装最新版本的 Compos ...

  2. br_netfilter 模块开机自动方法

    环境 cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) 在/etc/sysctl.conf中添加: net.bridge.bri ...

  3. slf4j和log4j结合使用步骤

    使用slf4j的优点: 提供带参数的日志输出方法(SLF4J 1.7及以后版本). pom中只需引入slf4j-log4j12,然后maven会引入它所依赖的其它JAR包. slf4j和log4j结合 ...

  4. F110使用的函数

    BAPI_ACC_DOCUMENT_POST BAPI_GL*POST 1.F-59 [没有找到函数]BAPI_ACC_DOCUMENT_POST 必须创建有借贷2 line 的凭证,需求要参考原始的 ...

  5. 1.4 使用电脑测试MC20的接收英文短信功能

    需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...

  6. sql备份命令

    --将SQL脚本赋值给变量 ) set @SqlBackupDataBase=N'BACKUP DATABASE dbname TO DISK = ''E:\DBBackup\dbname-'+ ), ...

  7. 《高级程序设计》8 BOM

    window对象 location对象 navigator对象 screen对象 history对象 一.window对象 BOM的核心对象是window,它表示浏览器的一个实例.在浏览器中,wind ...

  8. 【TECH】CAS php客户端配置

    搞完java又搞php,我整个人都不好了=.= 跟大师在linux上折腾了一下午,没调出来,早上在windows上跑通了,中午终于在linux上搞定了,嘿嘿. server端配置参见这里 在windo ...

  9. Java智能图表类库JChartLib使用介绍

    http://www.codeceo.com/article/java-jchartlib.html JChartLib是一款基于Java的智能图表类库,JChartLib不仅有着漂亮的外观,而且支持 ...

  10. [NOI2008]设计路线

    题目 洛谷 BZOJ 做法 神仙题 显然这是棵树 个节点相东仅连接一个结点 不同于剖分,还能存在\("V"\)字型,一个节点最多与另外节点连两条边 \(dp[i][j][k]\)表 ...