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)
#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 100Const intinf=0x7fffffff;//infinitely Largestructdota{intx; inty; intPre;} haha[ +];intdx[4]={1,-1,0,0};intdy[4]={0,0,1,-1};//int dp[10][10];intvis[ -][ -];voidPrintintx) { if(haha[x].pre!=-1) {print (haha[x].pre); printf ("(%d,%d) \ n", HAHA[X].X,HAHA[X].Y); }}voidBFsintXinty) {haha[0].x=x; haha[0].y=y; haha[0].pre=-1; intstart=0; intFina=1; intm=1; while(start<Fina) { for(intI=0;i<4; i++) { inta=haha[start].x+Dx[i]; intb=haha[start].y+Dy[i]; if(a<0|| A>4) Continue; if(b<0|| B>4) Continue; if(vis[a][b]==1) Continue; Fina++; VIS[A][B]=1; haha[m].x=A; Haha[m].y=b; Haha[m++].pre=start; if(a==4&&b==4) {print (start); }} Start++; }}intMain () { for(intI=0;i<5; i++) for(intj=0;j<5; j + +) {cin>>Vis[i][j]; } printf ("(0, 0) \ n"); BFS (0,0); printf ("(4, 4) \ n"); return 0;}
Maze problem Simulation Queue Breadth First search