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. python并发编程&多线程(二)

    前导理论知识见:python并发编程&多线程(一) 一 threading模块介绍 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性 官网链 ...

  2. Linux用户相关文件之密码文件

    1.文件地址: /etc/shadow ----------. 1 root root 842 10月 6 13:09 /etc/shadow 2.文件内容: xiaol_1:$6$NdCAnK3y$ ...

  3. 安卓3d引擎

    很 多初学Android游戏开发 href="http://edu.gamfe.com/gamedev.html">游戏开发的朋友,往往会显得有些无所适从.他们经常不知道该从 ...

  4. 从SignalTap II中获取“最真实”的仿真测试向量(ZZ)

         在实际工作中,经常会遇到这样的情况:在硬件调试中采用SignalTap II反复多次编译并最终捕获到问题的原因时,才会发现,原来这个问题是逻辑问题,是可以在仿真环境下发现并快速解决的.先前没 ...

  5. Python的模块与函数以及与自动化的结合

    3 模块与函数 3.1程序结构 python的程序由package,module,function组成,分别是包,模块,函数.模块是函数和类的集合,包,模块,函数之间的关系如下: 3.2模块 pyth ...

  6. LeetCode:学生的出勤记录|【551】

    LeetCode:学生的出勤记录|[551] 题目描述 给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个字符: 'A' : Absent,缺勤 'L' : Late,迟到 'P' : P ...

  7. hadoop linux 杂记

    切换到root        su    修改sudo sudo + 命令 --> root权限 + 命令        su root        vim /etc/sudoers      ...

  8. eclipse连接SqlServer2008(被它搞得惨兮兮)

    建民大叔告诉我要考试做一个系统要求连接SqlServer2008,于是我便开始了“炼狱”,人家连接起来一路绿灯,我却一路红灯所以决定把它记录下来,给后来人提供方便. 第一个红灯: 启动服务后利用cmd ...

  9. springboot-数据库

    Spring-data-jpa jpa定义了一系列持久化的标准,比如hibernate就实现了这一标准. Springboot 的jpa就是hibernate的整合. 在pom文件中增加配置: < ...

  10. JDK源码 - ArrayList (基于1.7)

    前言   推荐一位大牛的博客: https://blog.csdn.net/eson_15/article/details/51121833 我基本都是看的他的源码分析,刚开始如果直接看jdk源码可能 ...