- maze POJ - 3984 bfs record output path and the shortest path

The definition of 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 labyrinth, in which 1 represents the wall, 0 way to go, can only go sideways or vertically to go, can not go sideways, requiring programmed to find the shortest route from the top left to the bottom right corner.

Input

A 5 × 5 two-dimensional array, showing a maze. Ensure data has a unique solution.

Output

Left to bottom right shortest path, the format as shown in the sample.

Sample Input

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

Sample Output

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

 

And outputs the recording path

 

Code:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<iostream>
 5 #include<queue>
 6 using namespace std;
 7 struct shu
 8 {
 9     int y,x,step;
10 }str1,str2;
11 int a,s,d,f,g,q[10][10],v[10][10],w[10][10],z[25],c[25];
12 int p[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
13 void bfs()
14 {
15     int xx,yy,x,y;
16     queue<shu>r;
17     r.push(str1);
18     while(!r.empty())
19     {
20         str1=r.front();
21         r.pop();
22         if(str1.y==4 && str1.x==4)
23         {
24             xx=q[str1.y][str1.x]%10;
25             yy=(q[str1.y][str1.x]-xx)/10;
26             while(1)  //while循环找寻路径
27             {
28                 z[g]=yy;c[g]=xx;
29                 g++;
30                 if(q[yy][xx]==0)
31                 {
32                     break;
33                 }
34                 x=q[yy][xx]%10 ;    // find the point of a previous point coordinates (because we had previously maintained, so the same manner to find coordinates) 
35                  Y = (Q [YY] [XX]% 100 the -X-) / 10 ;
 36                  YY = Y;
 37 [                  XX = X;
 38 is              }
 39              the while (! r.empty ())
 40                  r.pop ();
 41 is              return ;
 42 is          }
 43 is          str2.step = str1.step + . 1 ;
 44 is          for ( int I = 0 ; I < . 4 ; ++ I)
 45         {
 46 is  
47              str2.y str1.y + P = [I] [ . 1 ];
 48              str2.x str1.x + P = [I] [ 0 ];
 49              IF (str2.y < 0 || str2.x < 0 || str2.y> = . 5 || str2.x> = . 5 ) Continue ;
 50              IF (V [str2.y] [str2.x] == 0 && W [str2.y] [str2.x] = = 0 )
 51 is              {
 52 is                  Q [str2.y] [str2.x] = str1.y * 10 + str1.x;   // put it to the previous point coordinates to maintain a unique value 
53 is                  W [str2.y] [str2.x] =. 1 ;
 54 is                  r.push (str2);
 55                  / * 
56 is                  in addition to this a way, we can define a special storage structure coordinates of a point
 57 is                  
58                  * / 
59              }
 60          }
 61 is      }
 62 is  }
 63 is  int main ()
 64  {
 65      G = 0 ;
 66      for ( int I = 0 ; I < . 5 ; ++ I)
 67      {
 68          for ( int J = 0 ; J < . 5 ; ++j)
69         {
70             scanf("%d",&v[i][j]);
71         }
72     }
73     q[0][0]=0;
74     str1.x=0;
75     str1.y=0;
76     str1.step=0;
77     w[0][0]=1;
78     bfs();
79     printf("(0, 0)\n");
80     for(int i=g-1;i>=0;--i)
81     {
82         printf("(%d, %d)\n",z[i],c[i]);
83     }
84     printf("(4, 4)\n");
85     return 0;
86 }

Guess you like

Origin www.cnblogs.com/kongbursi-2292702937/p/12044131.html