Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 2548 | Accepted: 1141 | Special Judge |
Description
In this problem you have to solve a very simple maze consisting of:
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
这道迷宫题与以前见过的不同,因为它的障碍物是隔板,但还是可以处理一下化成我们熟悉的模型。
可以用map[7][7][4]来描述迷宫,其中map[x][y][dir]表示从(x,y)沿dir所指方向移动到下一个位置是否可行,根据隔板位置就可以初始化map数组了。剩下的就是一个简单的BFS了。
#include <iostream> #include<queue> #include<cstdio> using namespace std; struct st { int x,y,step; char path[40]; }w,v; bool visit[7][7]; bool map[7][7][4];//记录从当前位置到下一个位置是否可达 int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};//上、右、下、左 void init() { int i,j,k; for(i=1;i<=6;i++) for(j=1;j<=6;j++) { visit[i][j]=false; for(k=0;k<4;k++) map[i][j][k]=true; } } char change(int i) { switch(i) { case 0:return 'N'; case 1:return 'E'; case 2:return 'S'; case 3:return 'W'; } } void bfs(int sx,int sy,int ex,int ey) { w.x=sx; w.y=sy; w.step=0; queue<st>q; q.push(w); visit[w.x][w.y]=true; while(!q.empty()) { w=q.front(); q.pop(); for(int i=0;i<4;i++) { v.step=w.step+1; v.x=w.x+dir[i][0]; v.y=w.y+dir[i][1]; if(v.x>0&&v.x<=6&&v.y>0&&v.y<=6&&map[w.x][w.y][i]&&!visit[v.x][v.y]) { if(v.x==ex&&v.y==ey) { for(int j=1;j<=w.step;j++) printf("%c",w.path[j]); printf("%c\n",change(i)); return; } visit[v.x][v.y]=true; for(int j=1;j<=w.step;j++) v.path[j]=w.path[j]; v.path[v.step]=change(i); q.push(v); } } } } int main() { int sx,sy,ex,ey; while(scanf("%d%d",&sy,&sx),sx+sy) { init(); scanf("%d%d",&ey,&ex); int x1,y1,x2,y2; for(int i=0;i<3;i++) { scanf("%d%d%d%d",&y1,&x1,&y2,&x2); if(y1==y2) { if(x1>x2) swap(x1,x2); for(int j=x1+1;j<=x2;j++) { if(y1>0) map[j][y1][1]=false; if(y1<6) map[j][y1+1][3]=false; } } else { if(y1>y2) swap(y1,y2); for(int j=y1+1;j<=y2;j++) { if(x1>0) map[x1][j][2]=false; if(x1<6) map[x1+1][j][0]=false; } } } bfs(sx,sy,ex,ey); } return 0; }