Description

You are trapped in a 3D3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?

Input

The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L,RL,R and CC (all limited to 3030 in size).
L is the number of levels making up the dungeon.
RR and CC are the number of rows and columns making up the plan of each level.
Then there will follow LL blocks of RR lines each containing CC characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a ‘#’ and empty cells are represented by a ‘.’. Your starting position is indicated by ‘S’ and the exit by the letter ‘E’. There’s a single blank line after each level. Input is terminated by three zeroes for L,RL, R and CC.

Output

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).
where xx is replaced by the shortest time it takes to escape.

If it is not possible to escape, print the line
Trapped!

Sample Input

3 4 5
S/./././.
/./#/#/#/.
/./#/#/./.
/#/#/#/./#

/#/#/#/#/#
/#/#/#/#/#
/#/#/./#/#
/#/#/././.

/#/#/#/#/#
/#/#/#/#/#
/#/./#/#/#
/#/#/#/#E

1 3 3
S/#/#
/#E/#
/#/#/#

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

Translation

3D3D迷宫,每走一步要花费一分钟,地图上标记为’#‘的点不可走,标记为’.'的点可走,问至少花费几分钟走出迷宫。

Idea

数据范围较小,经典的BFSBFS走迷宫问题。
需要注意的问题如下:

  • visvis数组标记访问
  • 判断可走条件
  • 定义位移矢量

Code

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
103
104
105
106
#include<queue>
#include<iostream>
#include<cstring>
using namespace std;
struct state
{
int z,x,y;
int step;
};
queue<state>q;
bool flag;//可逃出迷宫的标记
//位移矢量
const int dz[]={-1,1,0,0,0,0};
const int dx[]={0,0,-1,1,0,0};
const int dy[]={0,0,0,0,-1,1};
const int N=32;
char map[N][N][N];
bool vis[N][N][N];
int l,r,c;
void init()//初始化
{
queue<state>e;
swap(e,q);
memset(vis,0,sizeof(vis));
flag=0;
}
void bfs(state start,state end)
{
q.push(start);
vis[start.z][start.x][start.y]=1;
while(!q.empty())
{
state now;
now=q.front();
int i;
state nxt;
for(i=0;i<6;i++)
{
nxt.z=now.z+dz[i];
nxt.x=now.x+dx[i];
nxt.y=now.y+dy[i];
//超出迷宫边界
if(nxt.z<1||nxt.z>l||nxt.x<1||nxt.x>r||nxt.y<1||nxt.y>c) continue;
//该点已经走过
else if(vis[nxt.z][nxt.x][nxt.y]) continue;
//该点为障碍物
else if(map[nxt.z][nxt.x][nxt.y]=='#') continue;
else
{
nxt.step=now.step+1;
if(nxt.z==end.z&&nxt.x==end.x&&nxt.y==end.y)
{
flag=1;
printf("Escaped in %d minute(s).\n",nxt.step);
return;
}
else
{
vis[nxt.z][nxt.x][nxt.y]=1;//标记为走过
q.push(nxt);
}
}
}
q.pop();//队头的所有子节点已入队,队头出队。
}
}
void solve()
{
init();
state start,end;
int i,j,k;
for(k=1;k<=l;k++)
{
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
cin>>map[k][i][j];
if(map[k][i][j]=='S')
{
start.z=k;
start.x=i;
start.y=j;
start.step=0;
}
if(map[k][i][j]=='E')
{
end.z=k;
end.x=i;
end.y=j;
}
}
}
}
bfs(start,end);
if(!flag) puts("Trapped!");
}
int main()
{
while(cin>>l>>r>>c)
{
if(!l&&!r&&!c) break;
else solve();
}
return 0;
}