Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

Description

You are trapped in a 3D 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, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C 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, R and C.

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 x 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!


Source

AC代码：
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int dx[] = {1, 0, 0, -1, 0, 0},
dy[] = {0, 1, 0, 0, -1, 0},
dz[] = {0, 0, 1, 0, 0, -1};
struct node
{
int x, y, z, step;
bool operator == (const node& b) const
{
return x == b.x && y == b.y && z == b.z;
}
}start, end;
bool map;
int l, r, c;
void input()
{
char ch;
memset(map, 0, sizeof(map));
for (int i = 0; i < l; i++)
for (int j = 0; j < r; j++)
{
scanf("%s", ch);
for (int k = 0; k < c; k++)
{
if (ch[k] == 'S')
{
map[i][j][k] = 1;
start.x = i;
start.y = j;
start.z = k;
start.step = 0;
}
if (ch[k] == 'E')
{
map[i][j][k] = 1;
end.x = i;
end.y = j;
end.z = k;
end.step = 0;
}
if (ch[k] == '.')
map[i][j][k] = 1;
}
}

}
void bfs()
{
queue <node> q;
q.push(start);
while (!q.empty())
{
node u = q.front();
q.pop();
if (u == end)
{
printf("Escaped in %d minute(s).\n", u.step);
return;
}
int nowx = u.x, nowy = u.y, nowz = u.z;
if (!map[nowx][nowy][nowz] ||
nowx < 0 || nowx > l ||
nowy < 0 || nowy > r ||
nowz < 0 || nowz > c)
continue;
map[nowx][nowy][nowz] = 0;
node next;
for (int i = 0; i < 6; i++)
{
next.x = nowx + dx[i];
next.y = nowy + dy[i];
next.z = nowz + dz[i];
next.step = u.step + 1;
q.push(next);
}
}
printf("Trapped!\n");
}
int main()
{
while (scanf("%d%d%d", &l, &r, &c) && !(!l && !r && !c))
{
input();
bfs();
}
return 0;
}