Maze problem
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 15154 |
|
Accepted: 9030 |
Description
Define a two-dimensional array:
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, 0,
};
It represents a maze, of which 1 represents a wall, 0 means that the road can be walked, can only walk sideways or vertical walk, can not be inclined to walk, asked to compile the program to find the shortest route from the upper left to the lower right corner.
Input
A 5x5 two-dimensional array that represents a maze. The data guarantee has a unique solution.
Output
The shortest path in the upper-left corner to the lower-right corner, formatted as shown in the sample.
Sample Input
0 1 0 0 00 1 0 1 00 0 0 0 00 1 1 1 00 0 0 1 0
Sample Output
(0, 0) (1, 0) (2, 0) (2, 1) (2, 2) (2, 3) (2, 4) (3, 4) (4, 4)
Note the print path.
#include <iostream>#include<string.h>using namespacestd;Const intmaxn=1000005;structnode{intY,x,pre; Node () {} node (intCyintCxintCPRE) {y=Cy; X=CX; Pre=CPRE; }}QUE[MAXN];intfront,rear;intmz[5][5];intvis[5][5];intdy[4]={0,1,0,-1};intdx[4]={1,0,-1,0};voidPrintintID) {Node now=Que[id]; if(now.pre==-1) {cout<<"("<<now.y<<", "<<now.x<<")"<<Endl; return ; } print (Now.pre); cout<<"("<<now.y<<", "<<now.x<<")"<<Endl;}voidBFs () {memset (Vis,0,sizeof(VIS)); Front=rear=0; Que[rear++]=node (0,0,-1); vis[0][0]=1; while(front!=rear) {Node now=que[front++]; if(now.y==4&&now.x==4) {print (front-1); return ; } for(intI=0;i<4; i++) { intny=now.y+Dy[i]; intnx=now.x+Dx[i]; if(0<=ny&&ny<5&&0<=nx&&nx<5&&!vis[ny][nx]&&mz[ny][nx]==0) {que[rear++]=node (ny,nx,front-1); VIS[NY][NX]=1; } } } }intMain () { for(intI=0;i<5; i++) for(intj=0;j<5; j + +) Cin>>Mz[i][j]; BFS (); return 0;}
POJ3984 (Maze problem)