-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDungeon Master.cpp
93 lines (83 loc) · 1.64 KB
/
Dungeon Master.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
这个题就是三维的bfs地图问题,一个模板而已。。。
#include <iostream>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
int n, m, num, sx, tx, sy, ty, sz, tz;;
char map1[33][33][33];
int d[33][33][33];
int dx[]= {1, 0, -1, 0, 0, 0};
int dy[]= {0, 1, 0, -1 ,0 ,0};
int dz[]= {0, 0, 0, 0, 1, -1};
struct fin {
int x;
int y;
int z;
};
int bfs() {
queue<fin> q;
for (int i = 0; i < num; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < m; k++) {
d[i][j][k] = INF;
}
}
}
d[sx][sy][sz] = 0;
fin Mic;
Mic.x = sx;
Mic.y = sy;
Mic.z = sz;
q.push(Mic);
while (q.size()) {
fin p = q.front();
q.pop();
if (p.x == tx && p.y == ty && p.z == tz) {
break;
}
for (int i = 0; i < 6; i++) {
int lx = p.x + dx[i];
int ly = p.y + dy[i];
int lz = p.z + dz[i];
if ((0 <= lx && lx < num) && (0 <= ly && ly < n) && (0 <= lz && lz < m) && map1[lx][ly][lz] != '#' && d[lx][ly][lz]==INF) {
Mic.x = lx;
Mic.y = ly;
Mic.z = lz;
q.push(Mic);
d[lx][ly][lz] = d[p.x][p.y][p.z] + 1;
}
}
}
return d[tx][ty][tz];
}
int main() {
while (cin >> num >> n >> m){
if (n==0 && m==0 && num == 0) {
break;
}
for (int i = 0; i < num; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < m; k++) {
cin >> map1[i][j][k];
if (map1[i][j][k] == 'S') {
sx = i;
sy = j;
sz = k;
}
if (map1[i][j][k]=='E') {
tx = i;
ty = j;
tz = k;
}
}
}
}
int ans = bfs();
if (ans == INF) {
cout << "Trapped!" << endl;
} else {
cout << "Escaped in " << ans << " minute(s)." << endl;
}
}
return 0;
}