【HDOJ】2102 A计划

BFS,不过有很多地方需要注意,比如传送机传送到另一个传送机。还有要注意格式。

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