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?
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.
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
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
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!
三维BFS练手:
#include<iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
struct point{
int x,y,z,step;
point(){}
point(int xx,int yy,int zz,int ss):x(xx),y(yy),z(zz),step(ss){}
};
#define N 35
int a[N][N][N];
bool vis[N][N][N];
int l,r,c;
int ex,ey,ez;
int vx[]={1,0,-1,0,0,0};
int vy[]={0,-1,0,1,0,0};
int vz[]={0,0,0,0,1,-1};
int main(){
while (scanf("%d%d%d",&l,&r,&c)!=EOF){
queue<point> q;
if (l==0 && r==0 && c==0)
break;
memset(vis,false,sizeof(vis));
memset(a,0,sizeof(a));
for (int i=1;i<=l;i++){
for (int j=1;j<=r;j++){
getchar();
for (int k=1;k<=c;k++){
char c=getchar();
if (c=='S'){
a[i][j][k]=0;
q.push(point(i,j,k,0));
//printf("start %d %d %d\n",i,j,k);
}else if (c=='#'){
a[i][j][k]=0;
}else if (c=='.'){
a[i][j][k]=1;
}else{
a[i][j][k]=1;
ex=i; ey=j; ez=k;
}
}
}
getchar();
}
//printf("%d %d %d\n",ex,ey,ez);
point now;
int x,y,z,step;
while (!q.empty()){
now=q.front();
q.pop();
x=now.x,y=now.y,z=now.z,step=now.step;
if (ex==x && ey==y && ez==z)
break;
for (int i=0;i<6;i++){
int tx=x+vx[i];
int ty=y+vy[i];
int tz=z+vz[i];
if (a[tx][ty][tz]){
a[tx][ty][tz]=0;
q.push(point(tx,ty,tz,step+1));
}
}
}
if (ex==x && ey==y && ez==z){
printf("Escaped in %d minute(s).\n",step);
}else{
printf("Trapped!\n");
}
}
return 0;
}