Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 2876 | Accepted: 1301 | 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
这道题目其实是很裸的BFS,只是加了一个限制条件,即在输入墙的时候,记录一下哪些方格的上下左右不可以走,这个可以开一个三维数组,其中第四维长度为4,分别表示上下左右是否有墙,注意一下,这道题目的行和列和正常的是相反的,处理时行变成列,列变成行。
记录路径可以在结构体里面加pre,我这里是多开了一个数组保存节点,其实不要这么做,因为队列里就保存了这些节点,仔细想想,可以省下一半空间。
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#define Maxn 10010
using namespace std;
struct point{
int x,y,pre,id,dir;
point(){}
point(int tx,int ty,int p,int dd,int d):x(tx),y(ty),pre(p),id(dd),dir(d){}
}p[Maxn],q[Maxn];
int maze[10][10][4];
int vis[10][10];
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
int tot,ex,ey;
char ch[Maxn];
char print(int x){
if(x==0) return 'N';
if(x==1) return 'S';
if(x==2) return 'W';
if(x==3) return 'E';
}
int bfs(point &r){
int s=0,e;
q[e=0]=r;
vis[r.x][r.y]=1;
while(s<=e){
point t=q[s++];
if(t.x==ex&&t.y==ey) {ch[0]=print(t.dir);return t.pre;}
for(int i=0;i<4;i++){
int tx=t.x+dx[i],ty=t.y+dy[i];
if(tx<=0||ty<=0||tx>6||ty>6||maze[t.x][t.y][i]||vis[tx][ty]) continue;
p[tot]=point(tx,ty,t.id,tot,i);
q[++e]=p[tot];
vis[tx][ty]=1;
tot++;
}
}
}
int main()
{
int sx,sy,a,b,c,d;
while(scanf("%d%d",&sy,&sx),sx){
scanf("%d%d",&ey,&ex);
p[0]=point(sx,sy,-1,0,0);
memset(maze,0,sizeof maze);
memset(vis,0,sizeof vis);
for(int i=0;i<3;i++){
scanf("%d%d%d%d",&a,&b,&c,&d);
if(a==c){
if(b>d) swap(b,d);
for(int j=b+1;j<=d;j++){
if(a) maze[j][a][3]=1;
if(a!=6) maze[j][a+1][2]=1;
}
}
if(b==d){
if(a>c) swap(a,c);
for(int j=a+1;j<=c;j++){
if(b) maze[b][j][1]=1;
if(b!=6) maze[b+1][j][0]=1;
}
}
}
tot=1;
int pre=bfs(p[0]);
tot=1;
while(pre!=-1){
ch[tot++]=print(p[pre].dir);
pre=p[pre].pre;
}
for(int j=tot-2;j>=0;j--)
printf("%c",ch[j]);
puts("");
}
return 0;
}