九度oj 题目1335:闯迷宫

题目描述:

sun所在学校每年都要举行电脑节,今年电脑节有一个新的趣味比赛项目叫做闯迷宫。
sun的室友在帮电脑节设计迷宫,所以室友就请sun帮忙计算下走出迷宫的最少步数。
知道了最少步数就可以辅助控制比赛难度以及去掉一些没有路径到达终点的map。
比赛规则是:从原点(0,0)开始走到终点(n-1,n-1),只能上下左右4个方向走,只能在给定的矩阵里走。

输入:

输入有多组数据。
每组数据输入n(0<n<=100),然后输入n*n的01矩阵,0代表该格子没有障碍,为1表示有障碍物。
注意:如果输入中的原点和终点为1则这个迷宫是不可达的。

输出:

对每组输入输出该迷宫的最短步数,若不能到达则输出-1。

样例输入:
2
0 1
0 0
5
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
0 1 1 1 0
1 0 1 0 0
样例输出:
2
8

开始用dfs做的,结果超时了
代码如下:
 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <string>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <cmath>
 7 #define MAX 102
 8 #define inf 100002
 9 
10 int dir[][2] = {{0,1},{0,-1},{-1,0},{1,0}};
11 int map[MAX][MAX];
12 int flag[MAX][MAX];
13 int n;
14 int min;
15 
16 void dfs(int x, int y, int cnt) {
17     if(x == n-1 && y == n-1) {
18         if(min > cnt) {
19             min = cnt;
20         }
21         return;
22     }
23     if(cnt > min) {
24         return;
25     }
26     for(int i = 0; i < 4; i++) {
27         int tempx = x + dir[i][0];
28         int tempy = y + dir[i][1];
29         if(tempx >= 0 && tempx < n && tempy >= 0 && tempy < n && flag[tempx][tempy] == 0 &&map[tempx][tempy] == 0) {
30             flag[tempx][tempy] = 1;
31             dfs(tempx, tempy, cnt+1); 
32             flag[tempx][tempy] = 0;
33         }
34     }
35 }
36 
37 int main(int argc, char const *argv[])
38 {
39     freopen("input.txt","r",stdin);
40     while(scanf("%d",&n) != EOF) {
41         for(int i = 0; i < n; i++) {
42             for(int j = 0; j < n; j++) {
43                 scanf("%d",&map[i][j]);
44             }
45         }
46         if(map[0][0] == 1|| map[n-1][n-1] == 1) {
47             puts("-1");
48             continue;
49         }
50         min = inf;
51         memset(flag,0,sizeof(flag));
52         dfs(0, 0, 0);
53         if(min != inf) {
54             printf("%d
", min);
55         }
56         else {
57             puts("-1");
58         }
59         
60     }
61     return 0;
62 }

后来改成bfs,代码如下

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <string>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <queue>
 7 #define MAX 102
 8 #define inf 100002
 9 
10 using namespace std;
11 
12 int dir[][2] = {{0,1},{0,-1},{-1,0},{1,0}};
13 int map[MAX][MAX];
14 int cnt[MAX][MAX];
15 int n;
16 int ans;
17 
18 struct Point{
19     int x;
20     int y;
21 };
22 
23 queue <Point> temp;
24 
25 void bfs() {
26     Point source;
27     source.x = 0, source.y = 0;
28     cnt[0][0] = 0;
29     temp.push(source);
30     while(!temp.empty()) {
31         Point tmp = temp.front();
32         temp.pop();
33         for(int i = 0; i < 4; i++) {
34             int tempx = tmp.x + dir[i][0];
35             int tempy = tmp.y + dir[i][1];
36             if(tempx >= 0 && tempx < n && tempy >= 0 && tempy < n && map[tempx][tempy] == 0) {
37                 int tcnt = cnt[tmp.x][tmp.y] + 1;
38                 if(tcnt < cnt[tempx][tempy]) {
39                     Point p;
40                     p.x = tempx;
41                     p.y = tempy;
42                     cnt[tempx][tempy] = tcnt;
43                     temp.push(p);
44                 }
45             }
46         }
47     }
48 }
49     
50 
51 int main(int argc, char const *argv[])
52 {
53     //freopen("input.txt","r",stdin);
54     while(scanf("%d",&n) != EOF) {
55         for(int i = 0; i < n; i++) {
56             for(int j = 0; j < n; j++) {
57                 scanf("%d",&map[i][j]);
58                 cnt[i][j] = inf;
59             }
60         }
61         if(map[0][0] == 1|| map[n-1][n-1] == 1) {
62             puts("-1");
63             continue;
64         }
65         bfs();
66         ans = cnt[n-1][n-1];
67         if(ans != inf) {
68             printf("%d
", ans);
69         }
70         else {
71             puts("-1");
72         }
73         
74     }
75     return 0;
76 }

竟然一次通过,不错不错

原文地址:https://www.cnblogs.com/jasonJie/p/5731122.html