【HDOJ】1728 逃离迷宫

题目大坑,注意行列顺序式反的,另外注意起点和终点相同。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 using namespace std;
 6 
 7 #define MAXN 105
 8 
 9 typedef struct node_st {
10     int x, y, d;
11     int t;
12     node_st(){}
13     node_st(int xx, int yy, int dd, int tt) {
14         x = xx; y = yy; d = dd; t = tt;
15     }
16 } node_st;
17 
18 char map[MAXN][MAXN];
19 char turns[MAXN][MAXN];
20 int direct[4][2]={-1,0,1,0,0,-1,0,1};
21 int n, m, maxt;
22 
23 int bfs(int x, int y, int ex, int ey) {
24     int i, j, t;
25     queue<node_st> que;
26     node_st node;
27     memset(turns, 0x3f, sizeof(turns));
28     map[x][y] = '*';
29     que.push(node_st(x,y,5,0));
30 
31     while ( !que.empty() ) {
32         node = que.front();
33         que.pop();
34         for (i=0; i<4; ++i) {
35             x = node.x + direct[i][0];
36             y = node.y + direct[i][1];
37             if (x<0 || x>=n || y<0 || y>=m || map[x][y]=='*')
38                 continue;
39             if (i == node.d)
40                 t = node.t;
41             else
42                 t = node.t+1;
43             if (t > maxt+1)
44                 continue;
45             if (x==ex && y==ey)
46                 return 1;
47             if (t <= turns[x][y]) {
48                 que.push(node_st(x,y,i,t));
49                 turns[x][y] = t;
50             }
51         }
52     }
53 
54     return 0;
55 }
56 
57 int main() {
58     int case_n, bx, by, ex, ey;
59     int i;
60 
61     scanf("%d", &case_n);
62 
63     while (case_n--) {
64         scanf("%d %d", &n, &m);
65         for (i=0; i<n; ++i)
66             scanf("%s", map[i]);
67         scanf("%d%d%d%d%d",&maxt,&by,&bx,&ey,&ex);
68         if (bx==ex && by==ey) {
69            printf("yes
");
70            continue;
71         }
72         i = bfs(bx-1,by-1,ex-1,ey-1);
73         if (i)
74             printf("yes
");
75         else
76             printf("no
");
77     }
78 
79     return 0;
80 }
原文地址:https://www.cnblogs.com/bombe1013/p/3781070.html