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 <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int j,k,l,n;
}h[100000000];
char i[35][35][35];
int book[35][35][35];
int flag,num,a,b,c;
void kk(int j,int k,int l)
{
int tt=0,f=1,e,x,y,z;
while(tt<=f)
{
++tt;
if(i[h[tt].j][h[tt].k][h[tt].l]=='E')
{
flag=1;
num=h[tt].n;
break;
}
for(e=1;e<=6;e++)
{
x=h[tt].j;
y=h[tt].k;
z=h[tt].l;
if(e==1)
{
x=x+1;
}
else if(e==2)
{
x=x-1;
}
else if(e==3)
{
y=y+1;
}
else if(e==4)
{
y=y-1;
}
else if(e==5)
{
z=z+1;
}
else if(e==6)
{
z=z-1;
}
if(x<1||x>a||y<1||y>b||z<1||z>c)
continue;
if(book[x][y][z]==0&&(i[x][y][z]=='.'||i[x][y][z]=='E'))
{
h[++f].j=x;
h[f].k=y;
h[f].l=z;
h[f].n=h[tt].n+1;
book[x][y][z]=1;
}
}
}
}
int main()
{
int d,e,f,aa,bb,cc;
while(~scanf("%d %d %d",&a,&b,&c))
{
memset(book,0,sizeof(book));
if(a==0&&b==0&&c==0)
break;
getchar();
for(d=1;d<=a;d++)
{
for(e=1;e<=b;e++)
{
for(f=1;f<=c;f++)
{
scanf("%c",&i[d][e][f]);
if(i[d][e][f]=='S')
{
h[1].j=d;
h[1].k=e;
h[1].l=f;
h[1].n=0;
book[d][e][f]=1;
aa=d;
bb=e;
cc=f;
}
}
getchar();
}
getchar();
}
flag=0;
kk(aa,bb,cc);
if(flag)
printf("Escaped in %d minute(s).\n",num);
else printf("Trapped!\n");
}
return 0;
}