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
| #include <iostream>
#include <memory.h>
#include <string>
#include <queue>
using namespace std;
const int MAX = 32;
const int dx[] = { 1, -1, 0, 0, 0, 0 };
const int dy[] = { 0, 0, 1, -1, 0, 0 };
const int dz[] = { 0, 0, 0, 0, 1, -1 };
struct Point
{
Point(int _x = 0, int _y = 0, int _z = 0, int _ans = 0)
{ x = _x; y = _y; z = _z; ans = 0; }
int x, y, z, ans;
};
queue<Point> Q;
Point Start, End;
int pMaze[MAX][MAX][MAX];
bool pVisited[MAX][MAX][MAX];
int main()
{
int L, R, C;
string strTmp;
while(cin >> L >> R >> C)
{
if(L == 0 && R == 0 && C == 0) { break; }
while(!Q.empty()) { Q.pop(); }
for(int i = 1; i <= L; i++)
{
for(int j = 1; j <= R; j++)
{
cin >> strTmp;
for(int k = 1; k <= C; k++)
{
if(strTmp[k - 1] == 'S') { pMaze[i][j][k] = 0; Start = Point(i, j, k); }
else if(strTmp[k - 1] == 'E') { pMaze[i][j][k] = 0; End = Point(i, j, k); }
else if(strTmp[k - 1] == '.') { pMaze[i][j][k] = 0; }
else if(strTmp[k - 1] == '#') { pMaze[i][j][k] = 1; }
}
}
}
Q.push(Start);
bool bFlag = false;
memset(pVisited, false, sizeof(pVisited));
pVisited[Start.x][Start.y][Start.z] = true;
while(!Q.empty())
{
Point Now = Q.front(); Q.pop();
if(Now.x == End.x && Now.y == End.y && Now.z == End.z) { cout << "Escaped in " << Now.ans << " minute(s)." << endl; bFlag = true; break; }
for(int i = 0; i < 6; i++)
{
Point Next;
Next.x = Now.x + dx[i]; Next.y = Now.y + dy[i]; Next.z = Now.z + dz[i]; Next.ans = Now.ans + 1;
if(Next.x >= 1 && Next.x <= L && Next.y >= 1 && Next.y <= R && Next.z >= 1 && Next.z <= C &&
pMaze[Next.x][Next.y][Next.z] == 0 && !pVisited[Next.x][Next.y][Next.z])
{
pVisited[Next.x][Next.y][Next.z] = true;
Q.push(Next);
}
}
}
if(!bFlag) { cout << "Trapped!" << endl; }
}
return 0;
}
|