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
Output
Escaped in 11 minute(s).
Trapped!
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<string.h>
using namespace std;
const int N =50 ;
char Map[N][N][N];
int dz[] = {-1,1,0,0,0,0};
int dx[] = {0,0,-1,1,0,0};
int dy[] = {0,0,0,0,-1,1};
int u,n,m;
int a,b,c;
int d,e,f;
bool vis[N][N][N];
struct que
{
int z,x,y,t;
};
queue<que> q;
bool check(int z,int x,int y)
{
if(z<0||z>=u||x<0||x>=n||y<0||y>=m) return false;//越界
if(vis[z][x][y]) return false;//已经走过
if(Map[z][x][y]=='#') return false;//墙壁
return true;
}
void bfs()
{
que temp;
temp.z = a;
temp.x = b;
temp.y = c;
temp.t = 0;
q.push(temp);
vis[a][b][c] = true;
while(!q.empty())
{
temp = q.front();
q.pop();
for(int i=0;i<6;i++)
{
int zz = temp.z+dz[i];
int xx = temp.x+dx[i];
int yy = temp.y+dy[i];
if(!check(zz,xx,yy)) continue;//非法
if(zz==d&&xx==e&&yy==f)//到达终点
{
printf("Escaped in %d minute(s).\n",temp.t+1);
return;
}
que tt;
tt.z = zz;
tt.x = xx;
tt.y = yy;
tt.t = temp.t+1;
q.push(tt);
vis[zz][xx][yy] = true;
}
}
printf("Trapped!\n");
}
int main()
{
while(scanf("%d %d %d",&u,&n,&m)!=EOF)
{
if(u==0&&n==0&&m==0) break;
memset(vis,false,sizeof(vis));
q = queue<que>();//初始化
for(int i=0;i<u;i++)
{
for(int j=0;j<n;j++)
{
scanf("%s",Map[i][j]);
for(int k=0;k<m;k++)
{
if(Map[i][j][k]=='S')//记录起点
{
a = i;
b = j;
c = k;
}
if(Map[i][j][k]=='E')//记录终点
{
d = i;
e = j;
f = k;
}
}
}
getchar();
}
bfs();
}
return 0;
}