HDU 1484 Basic wall maze

来源:互联网 发布:mac 中文字幕乱码 编辑:程序博客网 时间:2024/05/21 05:05


Basic wall maze

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 429    Accepted Submission(s): 161
Special Judge


Problem 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 62 60 0 1 01 5 1 61 5 3 50 0
 

Sample Output
NEEESWW
 

Source
2006/2007 ACM-ICPC University of Ulm Local Contest
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1490 1485 1489 1488 1307 
 

Statistic | Submit | Discuss | Note


要记忆路径,用个字符串记忆,BFS


#include<stdio.h>#include<string.h>#include<stdlib.h>#include<algorithm>#include<queue>using namespace std;struct point{int x,y,num;char step[40];//记忆路径 };int dir[4][2]={0,-1,1,0,0,1,-1,0}; //上右下左 int ex,ey;int map[10][10][4];int vis[10][10];void bfs(point p){int i;queue<point>q;if(p.x==ex&&p.y==ey) {              printf("\n");              return ;       }q.push(p); vis[p.x][p.y]=1;point next;while(!q.empty()){p=q.front(); q.pop();for(i=0;i<4;i++){int x=p.x+dir[i][0],y=p.y+dir[i][1];if(!map[p.x][p.y][i]&&!vis[x][y]&&x>=1&&y>=1&&x<=6&&y<=6){next.x=x; next.y=y; strcpy(next.step,p.step);next.num=p.num+1;vis[next.x][next.y]=1;if(i==0)next.step[next.num-1]='N';                else if(i==1) next.step[next.num-1]='E';                else if(i==2) next.step[next.num-1]='S';                else next.step[next.num-1]='W';    q.push(next);if(next.x==ex&&next.y==ey){                    next.step[next.num]='\0';                    puts(next.step);                    return ;                }}}} return ;}int main(){int i,x1,x2,y1,y2,j,k;point star;while(scanf("%d%d",&star.x,&star.y)&&star.x&&star.y){memset(map,0,sizeof(map));memset(vis,0,sizeof(vis));scanf("%d%d",&ex,&ey);for(j=0;j<3;j++){  //标记不可通方向scanf("%d%d%d%d",&x1,&y1,&x2,&y2);if(x1==x2){for(i=y1+1;i<=y2;i++){map[x1][i][1]=1;map[x1+1][i][3]=1;}} else if(y1==y2){for(i=x1+1;i<=x2;i++){map[i][y1][2]=1;map[i][y1+1][0]=1;}}}star.num=0;bfs(star); }return 0;} 



0 0
原创粉丝点击