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 lineTrapped!Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0Sample Output
Escaped in 11 minute(s). Trapped!
#include<stdio.h>
#include<string.h>
#include<queue>///bfs用队列实现
using namespace std;
char map[31][31][31];///精确开辟地图和记录数组
int book[31][31][31];
int l,r,c,startx,starty,startz,endx,endy,endz;
int nextstep[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
///三维坐标系朝六个方向走,前后左右上下
struct node
{
int x,y,z,step;
}a;///注意!!!next结构体不能在此定义
int check(int x,int y,int z)
{
if(x<0 || y<0 || z<0 || x>=l || y>=r || z>=c)
return 1;///越界不行
else if(map[x][y][z] == '#')
return 1;///石壁不行
else if(book[x][y][z])
return 1;///已经走过
return 0;
}
int bfs()
{
node next;
queue<node>q;
a.x = startx,a.y = starty,a.z = startz;///定义头节点
a.step = 0;///开始步数为0
book[startx][starty][startz] = 1;///很关键!!!设置该点已入过队
q.push(a);///节点入队
while(!q.empty())
{///判断队列是否为空
a = q.front();///取出队首元素
q.pop();///队首元素出队
if(a.x == endx && a.y == endy && a.z == endz)
return a.step;///找到终点返回步数
for(int i = 0; i<6; i++)
{///从六个方向搜索
next.x = a.x+nextstep[i][0];
next.y = a.y+nextstep[i][1];
next.z = a.z+nextstep[i][2];
if(check(next.x,next.y,next.z))
continue;///当前的位置不符合就过
book[next.x][next.y][next.z] = 1;///标记位置已经入队
next.step = a.step+1;///继承前一步并加1
q.push(next);///将节点入队
}
}
return 0;///找不到返回的是0
}
int main()
{
int i,j,k;
while(scanf("%d%d%d",&l,&r,&c),l+r+c)
{///l,r,c不能都为0
for(i = 0; i<l; i++)
{
for(j = 0; j<r; j++)
{///输入地图
scanf("%s",&map[i][j]);
for(k = 0; k<c; k++)
{
if(map[i][j][k] == 'S')///找到开始的点
startx = i,starty = j,startz = k;
if(map[i][j][k] == 'E')///找到结束的点
endx = i,endy = j,endz = k;
}
}
}///非常关键,book数组要全置0!!!
memset(book,0,sizeof(book));
int ans = bfs();
if(ans)///如果ans不是0,说明能走出来,输出步数(时间)
printf("Escaped in %d minute(s).\n",ans);
else
printf("Trapped!\n");
}
}
inline char gc()
{
static char buff[100000000], *S = buff, *T = buff;
return S == T && (T = (S = buff) + fread(buff, 1, 100000000, stdin), S == T) ? EOF : *S++;
}