-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVa532.cpp
102 lines (75 loc) · 1.83 KB
/
UVa532.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
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
int moves[6][3] = {
{0,1,0},
{1,0,0},
{0,0,1},
{0,-1,0},
{-1,0,0},
{0,0,-1}
};
struct Point
{
int x;
int y;
int z;
};
char dungeons[31][31][31];
int walk[31][31][31];
int l,r,c;
int BFS(int startPos[]) {
queue<struct Point> q;
struct Point start;
start.x = startPos[0];
start.y = startPos[1];
start.z = startPos[2];
dungeons[start.x][start.y][start.z] = '#';
q.push(start);
walk[start.x][start.y][start.z] = 0;
while(!q.empty()) {
struct Point curPoint = q.front();
q.pop();
for(int i = 0 ; i < 6 ; i ++) {
struct Point nextPoint;
nextPoint.x = curPoint.x + moves[i][0];
nextPoint.y = curPoint.y + moves[i][1];
nextPoint.z = curPoint.z + moves[i][2];
if(nextPoint.x < 0 || nextPoint.x >= l ||
nextPoint.y < 0 || nextPoint.y >= r ||
nextPoint.z < 0 || nextPoint.z >= c) {
continue;
}
if(dungeons[nextPoint.x][nextPoint.y][nextPoint.z] == '#') continue;
walk[nextPoint.x][nextPoint.y][nextPoint.z] = walk[curPoint.x][curPoint.y][curPoint.z] + 1;
if(dungeons[nextPoint.x][nextPoint.y][nextPoint.z] == 'E')
return walk[nextPoint.x][nextPoint.y][nextPoint.z];
dungeons[nextPoint.x][nextPoint.y][nextPoint.z] = '#';
q.push(nextPoint);
}
}
return -1;
}
int main() {
while(scanf("%d %d %d", &l, &r, &c) != EOF) {
int startPos[3] = {0};
if(l == 0 || r == 0 || c == 0) break;
for(int i = 0 ; i < l ; i ++) {
for(int j = 0 ; j < r ; j ++) {
scanf("%s", dungeons[i][j]);
for(int k = 0 ; k < c ; k ++) {
if(dungeons[i][j][k] == 'S') {
startPos[0] = i;
startPos[1] = j;
startPos[2] = k;
}
}
}
}
int result = BFS(startPos);
if(result == -1) cout << "Trapped!" << endl;
else cout << "Escaped in "<< result <<" minute(s)." << endl;
}
return 0;
}