题目链接:https://vjudge.net/problem/POJ-2251
题意:简单的三维地图
思路:直接上代码。。。
1 #include <iostream>
2 #include <string.h>
3 #include<queue>
4 #include <algorithm>
5 using namespace std;
6
7 #define rep(i,j,k) for(int i = (j); i <= (k); i++)
8 #define per(i,j,k) for(int i = (j); i >= (k); i--)
9
10 const int N = 40;
11 int mv_x[] = { 0, 0, 0, 0, 1, -1 };
12 int mv_y[] = { 0, 0, 1, -1, 0, 0 };
13 int mv_z[] = { 1, -1, 0, 0, 0, 0 };
14 char mp[N][N][N];
15 bool vis[N][N][N];
16 int sx, sy, sz;//入口
17 int ex, ey, ez;//出口
18 int xx,yy,zz,ans;
19
20 struct node{
21 int x, y, z, c;
22 };
23
24 void init(){
25 rep(i, 1, zz) rep(j, 1, xx) rep(z, 1, yy){
26 vis[i][j][z] = false;
27 }
28 }
29
30 void input(){
31
32 rep(i, 1, zz) rep(j, 1, xx) rep(z, 1, yy){
33 cin >> mp[i][j][z];
34 if (mp[i][j][z] == 'S'){
35 sx = j; sy = z; sz = i;
36 }
37 else if (mp[i][j][z] == 'E'){
38 ex = j; ey = z; ez = i;
39 }
40 }
41 }
42
43 inline bool check(int x, int y, int z){
44 return x >= 1 && x <= xx
45 && y >= 1 && y <= yy
46 && z >= 1 && z <= zz;
47 }
48
49 bool bfs(){//true为能出去 false不能出去
50
51 queue<node> que;
52 que.push(node{ sx, sy, sz, 0 });
53
54
55 vis[sz][sx][sy] = true;
56
57 while (!que.empty()){
58
59 node tmp = que.front();
60 que.pop();
61
62 rep(p, 0, 5){
63 int dx = tmp.x + mv_x[p];
64 int dy = tmp.y + mv_y[p];
65 int dz = tmp.z + mv_z[p];
66
67 if (check(dx, dy, dz) && mp[dz][dx][dy] != '#' && !vis[dz][dx][dy]){
68
69 if (dx == ex && dy == ey && dz == ez){//到了出口
70 ans = tmp.c + 1;
71 return true;
72 }
73
74 vis[dz][dx][dy] = true;
75 que.push(node{ dx, dy, dz ,tmp.c + 1});
76 }
77 }
78 }
79
80 return false;
81 }
82
83 int main(){
84
85 ios::sync_with_stdio(false);
86 cin.tie(0);
87
88 while (cin >> zz >> xx >> yy){
89 if (zz == 0) break;
90
91 init();
92 input();
93
94 if (bfs()) cout << "Escaped in " << ans << " minute(s)." << endl;
95 else cout << "Trapped!" << endl;
96 }
97
98 return 0;
99 }