-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDungeon Master.cpp
73 lines (59 loc) · 1.84 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
#include <bits/stdc++.h>
using namespace std;
char dungeon[31][31][31];
int dist[31][31][31]={0};
int L,R,C;
const int direction[6][3]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}};
typedef struct point_type{
int x;
int y;
int z;
}point_type;
int bfs(int i,int j,int k){
dist[i][j][k]=0;
queue<point_type> q;
q.push(point_type{i,j,k});
dungeon[i][j][k]='#';
point_type cur,next;
while(!q.empty()){
cur = q.front();
q.pop();
for(int i=0;i<6;i++){
next.x = cur.x + direction[i][0];
next.y = cur.y + direction[i][1];
next.z = cur.z + direction[i][2];
if(next.x<0 || next.x>=L || next.y<0 || next.y>=R || next.z<0 || next.z>=C) continue;
if(dungeon[next.x][next.y][next.z] != '#'){
dist[next.x][next.y][next.z] = dist[cur.x][cur.y][cur.z]+1;
if(dungeon[next.x][next.y][next.z]=='E'){
return dist[next.x][next.y][next.z];
}
dungeon[next.x][next.y][next.z]='#';
q.push(next);
}
}
}
return -1;
}
int main(int argc, char** argv) {
while(cin>>L>>R>>C){
if(!L && !R && !C) break;
int start_i,start_j,start_k;
for(int i=0;i<L;i++){
for(int j=0;j<R;j++){
cin>>dungeon[i][j];
for(int k=0;k<C;k++){
if(dungeon[i][j][k] == 'S'){
start_i = i;
start_j = j;
start_k = k;
}
}
}
}
int minute = bfs(start_i,start_j,start_k);
if(minute != -1) cout << "Escaped in " << minute << " minute(s)." << endl;
else cout << "Trapped!" << endl;
}
return 0;
}