/*
三维bfs,三维数组维度从高到低为z,y,x
*/
三维bfs,三维数组维度从高到低为z,y,x
*/
#include <cstdio>
#include <cstring>
#include <queue>
#define N 35
#include <cstring>
#include <queue>
#define N 35
using namespace std;
typedef struct node {
int x, y, z;
int step;
}point;
int x, y, z;
int step;
}point;
int l, r, c;
char map[N][N][N];
int vis[N][N][N];
queue<point> q;//定义point型的队列
//上下,东西南北
int dx[6] = { 0,0,0,0,-1,1 };
int dy[6] = { 0,0,1,-1,0,0 };
int dz[6] = { 1,-1,0,0,0,0 };
char map[N][N][N];
int vis[N][N][N];
queue<point> q;//定义point型的队列
//上下,东西南北
int dx[6] = { 0,0,0,0,-1,1 };
int dy[6] = { 0,0,1,-1,0,0 };
int dz[6] = { 1,-1,0,0,0,0 };
int bfs(point s)
{
while (!q.empty()) q.pop();//初始化清空队列
q.push(s); vis[s.z][s.y][s.x] = 1;
while (!q.empty())
{
for (int i = 0; i < 6; i++)
{
point t;
t.x = q.front().x + dx[i]; t.y = q.front().y+dy[i]; t.z = q.front().z + dz[i]; t.step = q.front().step + 1;
if (t.x < 0 || t.x >= c || t.y < 0 || t.y >= r || t.z < 0 || t.z >= l)//越界
continue;
if (map[t.z][t.y][t.x] == '#' || vis[t.z][t.y][t.x])//障碍或已标记
continue;
if (map[t.z][t.y][t.x] == 'E')//到达终点
return t.step;
else
{
vis[t.z][t.y][t.x] = 1;
q.push(t);
}
}
q.pop();
}
{
while (!q.empty()) q.pop();//初始化清空队列
q.push(s); vis[s.z][s.y][s.x] = 1;
while (!q.empty())
{
for (int i = 0; i < 6; i++)
{
point t;
t.x = q.front().x + dx[i]; t.y = q.front().y+dy[i]; t.z = q.front().z + dz[i]; t.step = q.front().step + 1;
if (t.x < 0 || t.x >= c || t.y < 0 || t.y >= r || t.z < 0 || t.z >= l)//越界
continue;
if (map[t.z][t.y][t.x] == '#' || vis[t.z][t.y][t.x])//障碍或已标记
continue;
if (map[t.z][t.y][t.x] == 'E')//到达终点
return t.step;
else
{
vis[t.z][t.y][t.x] = 1;
q.push(t);
}
}
q.pop();
}
return -1;
}
}
int main()
{
while (scanf("%d%d%d", &l, &r, &c) != EOF && l && r && c)
{
getchar();
int ans = 0;
point start;
//输入迷宫,初始化标记数组
for (int i = 0; i < l; i++)
{
for (int j = 0; j < r; j++)
{
for (int k = 0; k < c; k++)
{
scanf("%c", &map[i][j][k]);
if (map[i][j][k] == 'S')
{
start.x = k; start.y = j; start.z = i; start.step = 0;
}
}
getchar();//吃换行
}
getchar();//吃换行
}
memset(vis, 0, sizeof(vis));
//执行bfs,输出结果
ans = bfs(start);
{
while (scanf("%d%d%d", &l, &r, &c) != EOF && l && r && c)
{
getchar();
int ans = 0;
point start;
//输入迷宫,初始化标记数组
for (int i = 0; i < l; i++)
{
for (int j = 0; j < r; j++)
{
for (int k = 0; k < c; k++)
{
scanf("%c", &map[i][j][k]);
if (map[i][j][k] == 'S')
{
start.x = k; start.y = j; start.z = i; start.step = 0;
}
}
getchar();//吃换行
}
getchar();//吃换行
}
memset(vis, 0, sizeof(vis));
//执行bfs,输出结果
ans = bfs(start);
if (ans == -1) printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n", ans);
}
else printf("Escaped in %d minute(s).\n", ans);
}
return 0;
}
}