4127:迷宫问题(bfs)

总时间限制: 

1000ms
 
内存限制: 
65536kB
描述

定义一个二维数组:

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,

};


它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

 

输入
一个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
样例输出
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

指针被我用着用着迷糊了。不过还是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     visit[0][0]=1;
33     q.push(new Node(0,0,NULL));
34     while(!q.empty()){
35         Node *p=q.front();
36         q.pop();
37         if(p->r==4&&p->c==4){
38             fun(p);
39             return 0;
40         }
41         else{
42             for(int i=0;i<4;i++){
43                 int rr=p->r+dr[i];
44                 int cc=p->c+dc[i];
45                 if(!visit[rr][cc]&&a[rr][cc]==0&&rr>=0&&rr<=4&&cc>=0&&cc<=4){                
46                     visit[rr][cc]=1;
47                     q.push(new Node(rr,cc,p));
48                 }
49             }
50             
51         }
52     }
53 
54     return 0;
55 }

 

下面的代码忘记转自哪里了,好理解点;

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <queue>
 5 
 6 using namespace std;
 7 struct node
 8 {
 9     int x,y;
10 }vis[6][6];
11 //用于存放路径 - 二维数组第一维存放当前路径的x、二维存放y
12 //.x存放上一个路径的x、.y存放上一个路径的y
13 int a[6][6],book[6][6];    //book用来标记
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                 book[next.x][next.y] = 1;
39                 vis[next.x][next.y] = now;    //记录上一个路径信息
40                 q.push(next);
41             }
42             if(next.x == 4 && next.y == 4)
43                 return;
44         }
45     }
46 }
47 //使用递归输出路径
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 }

 

posted @ 2020-04-03 19:42  瓜瓜爱呱呱  阅读(239)  评论(0编辑  收藏  举报