您的位置:首页 > 其它

Basic Wall Maze_条件bfs_2018_2_23

2018-02-23 21:04 363 查看
Basic Wall Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3257 Accepted: 1475 Special Judge
DescriptionIn 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.
InputThe 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.
OutputFor 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 Input1 6
2 6
0 0 1 0
1 5 1 6
1 5 3 5
0 0Sample OutputNEEESWWSourceUlm Local 2006
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;

#define place(x,y) x+y*6
const int dir[4][2]={0,-1,1,0,0,1,-1,0};
const char d[4]={'N','E','S','W'};
int start,ed;
bool no[7][7][4];
int vis[40];//保留前一个*10+方向

void output(int x){
if(x==start)return;
output(vis[x]/10);
printf("%c",d[vis[x]%10]);
}

int main(){
int x,y,tx,ty;
while(scanf("%d%d",&x,&y),x||y){
memset(vis,0,sizeof(vis));
memset(no,0,sizeof(no));
start=place(--x,--y);
scanf("%d%d",&x,&y);
ed=place(--x,--y);
for(int i=0;i<3;i++){
scanf("%d%d%d%d",&x,&y,&tx,&ty);
if((x==tx&&(x==0||x==6))||(y==ty&&(y==0||y==6)))continue;
if(x==tx)
for(;y<ty;y++)
no[x][y][3]=no[x-1][y][1]=1;
else for(;x<tx;x++)
no[x][y][0]=no[x][y-1][2]=1;
}
vis[start]=1;
queue<int> q;
q.push(start);
while(!q.empty()&&!vis[ed]){
int now=q.front();q.pop();
x=now%6;y=now/6;
for(int i=0;i<4;i++){
tx=x+dir[i][0];
ty=y+dir[i][1];
int nxt=place(tx,ty);
if(tx<0||tx>5||ty<0||ty>5||no[x][y][i]||vis[nxt])continue;
vis[nxt]=now*10+i;
q.push(nxt);
}
}
output(ed);
puts("");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: