Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26358 | Accepted: 10253 |
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解决,建一个3d空间
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//#include<malloc.h>
#include<math.h>
char map[31][31][31];
char book[31][31][31];
struct queue{
int x;
int y;
int z;
int step;
}q[1000000];
int l,r,c;
int nextx[6]={-1,1,0,0,0,0};
int nexty[6]={0,0,-1,1,0,0};
int nextz[6]={0,0,0,0,-1,1};
int main(){
while(~scanf("%d%d%d",&l,&r,&c)){
if(l==0&&r==0&&c==0)
break;
int i,j,k;
int head,tail;
int startx,starty,startz,endx,endy,endz;
for(i=0;i<l;i++){
for(j=0;j<r;j++){
scanf("%s",map[i][j]);
for(k=0;k<c;k++){
book[i][j][k]=0;
if(map[i][j][k]=='S'){
startx=j;starty=k;startz=i;
map[i][j][k]='.';
}
else if(map[i][j][k]=='E'){
endx=j;endy=k;endz=i;
map[i][j][k]='.';
}
}
}
getchar();
}
/*for(i=0;i<l;i++){
for(j=0;j<r;j++){
puts(map[i][j]);
}
//getchar();
}*/
head=0;tail=0;
q[head].x=startx;
q[head].y=starty;
q[head].z=startz;
q[head].step=0;
tail++;
book[startz][startx][starty]=1;
int xx,yy,zz;
int flag;
flag=0;
while(head<tail){
for(i=0;i<6;i++){
xx=q[head].x+nextx[i];
yy=q[head].y+nexty[i];
zz=q[head].z+nextz[i];
if(xx<0||xx>=r||yy<0||yy>=c||zz<0||zz>=l)
continue;
if(book[zz][xx][yy]==1)
continue;
if(map[zz][xx][yy]=='.'){
book[zz][xx][yy]=1;
q[tail].x=xx;
q[tail].y=yy;
q[tail].z=zz;
q[tail].step=q[head].step+1;
if(q[tail].x==endx&&q[tail].y==endy&&q[tail].z==endz){
flag=1;
break;
}
//printf("%d %d %d %d\n",q[tail].step,q[tail].x,q[tail].y,q[tail].z);
tail++;
}
}
if(flag)
break;
head++;
}
if(flag)
printf("Escaped in %d minute(s).\n",q[head].step+1);
else
printf("Trapped!\n");
for(i=0;i<l;i++){
for(j=0;j<r;j++){
for(k=0;k<c;k++){
book[i][j][k]=0;
map[i][j][k]='\0';
}
}
}
}
return 0;
}