# ACM 之 C - Dungeon Master

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

## 代码部分

``````#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
char map,cc;
int s1,s2,s3,x,y,z,a,b,c,finde,sum,fla;
struct node
{
int x,y,z;
};
queue<node>S;
node chan={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
int main()
{
while(scanf("%d%d%d",&a,&b,&c)&&a&&b&&c)
{
getchar();//吃换行符
memset(fla,0,sizeof(fla));
memset(map,'#',sizeof(map));//数据清空
for(int i=0;i<a;i++)
{   for(int j=0;j<b;j++)
{   for(int k=0;k<c;k++)
{
scanf("%c",&map[i][j][k]);//地图输入
if(map[i][j][k]=='.'||map[i][j][k]=='E')
fla[i][j][k]=-1;
if(map[i][j][k]=='S')
{
s1=i;s2=j;s3=k;
}
}getchar();
}getchar();
}
node thes;
thes.x=s1;thes.y=s2;thes.z=s3;
while(!S.empty())//队列清空
{
S.pop();
}
node count={-1,0,0};
S.push(count);
S.push(thes);
sum=-1;finde=0;
while(S.size()>1)//循环搜索出口
{
if(S.front().x == -1){ sum++; S.push(S.front()); S.pop(); }//层数标记
x = S.front().x; y = S.front().y; z = S.front().z;
if(map[x][y][z]=='E'){finde=1;break;}
node si;
for(int kk=0;kk<6;kk++)
{
if(fla[x+chan[kk].x][y+chan[kk].y][z+chan[kk].z]==-1)
{
si.x=x+chan[kk].x;
si.y=y+chan[kk].y;
si.z=z+chan[kk].z;
S.push(si);
fla[x+chan[kk].x][y+chan[kk].y][z+chan[kk].z]=1;//一定要在这里标记它！不然就可能超时。
}
}
S.pop();
}
if(finde==1)
{
printf("Escaped in %d minute(s).\n",sum);
}
else
printf("Trapped!\n");
}
return 0;
}
``````