4127: maze (bfs)

Total time limit: 

1000ms
 
Memory Limit: 
65536kB
description

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.

 

Entry
A 5 × 5 two-dimensional array, showing a maze. Ensure data has a unique solution.
Export
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) 

pointer is I with the use of the confused. But still the ac
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int a[6][6],visit[6][6];
 4 struct Node {
 5     int r,c;
 6     Node *pre;
 7 //    Node(int rr,int cc,Node *a):r(rr),c(cc),pre(a){}
 8 };
 9 int dr[4]={1,-1,0,0};
10 int dc[4]={0,0,1,-1};
11 queue <Node*> q; 
12 
13 void fun(Node *s){
14     if(s->r==0&&s->c==0){
15         printf("(%d, %d)\n",s->r,s->c);
16         return;
17     }
18     else{
19         fun(s->pre);
20         printf("(%d, %d)\n",s->r,s->c);
21     }
22 }
23 
24 int main() {
25     memset(visit,0,sizeof(visit));
26     for(int i=0; i<5; i++) {
27         for(int j=0; j<5; j++) {
28             cin>>a[i][j];
29         }
30     }
31     while(!q.empty())q.pop();
32     Node *start=new Node();
33     start->r=0;
34     start->c=0;
35     start->pre=NULL;
36     visit[0][0]=1;
37     q.push(start);
38     while(!q.empty()){
39         Node *p=q.front();
40         q.pop();
41         if(p->r==4&&p->c==4){
42             fun(p);
43             return 0;
44         }
45         else{
46             for(int i=0;i<4;i++){
47                 int rr=p->r+dr[i];
48                 int cc=p->c+dc[i];
49                 if(!visit[rr][cc]&&a[rr][cc]==0&&rr>=0&&rr<=4&&cc>=0&&cc<=4){
50                     Node *pp=new Node();
51                     pp->r=rr;
52                     pp->c=cc;
53                     pp->pre=p;
54                     visit[rr][cc]=1;
55                     q.push(pp);
56                 }
57             }
58             
59         }
60     }
61 
62     return 0;
63 }

The following code forget where switched from a good understanding of the point;

. 1 #include <stdio.h>
 2 #include <the iostream>
 . 3 #include <algorithm>
 . 4 #include <Queue>
 . 5  
. 6  the using  namespace STD;
 . 7  struct Node
 . 8  {
 . 9      int X, Y;
 10 } VIS [ . 6 ] [ 6 ];
 11  // for storing path - x dimension of the first two-dimensional array to store the current path, storing the two-dimensional Y
 12 is  // Y on the x .x stored on a path, a path .y storage 
13 is  int a [ . 6 ] [ . 6 ], Book [ . 6 ] [ . 6 ];     // Book for marking
14 int oper[4][2] = {
15     {0,1},
16     {1,0},
17     {0,-1},
18     {-1,0}
19 };
20 void bfs()
21 {
22     queue<node> q;
23     node now;
24     now.x = now.y = 0;
25     book[0][0] = 1;
26     q.push(now);
27     while(!q.empty())
28     {
29         now = q.front();
30         q.pop();
31         for(int i = 0;i < 4;i++)
32         {
33             node next;
34             next.x = now.x + oper[i][0];
35             next.y = now.y + oper[i][1];
36             if(next.x>=0 && next.x<. 5 && next.y> = 0 && next.y < . 5 &&! Book [next.x] [next.y] && A [next.x] [next.y]! = . 1 )
 37 [              {
 38 is                  Book [Next. X] [next.y] = . 1 ;
 39                  VIS [next.x] [next.y] = now;     // a path record information 
40                  q.push (Next);
 41 is              }
 42 is              IF (== next.x . 4 && next.y == . 4 )
 43 is                  return ;
 44 is          }
 45      }
 46 is  }
 47  // recursive output path
48 void print(int x, int y)
49 {
50     if(x == 0 && y == 0)
51         printf("(0, 0)\n");
52     else
53     {
54         print(vis[x][y].x, vis[x][y].y);
55         printf("(%d, %d)\n",x,y);
56     }
57 }
58 
59 int main()
60 {
61     int i,j;
62     for(i = 0;i < 5;i++)
63         for(j = 0;j < 5;j++)
64             scanf("%d",&a[i][j]);
65     bfs();
66     print(4, 4);
67     return 0;
68 }

 

Guess you like

Origin www.cnblogs.com/aiqinger/p/12628547.html