【搜索】Dungeon Master

时间:2022-04-24 07:28:40

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<iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 35; int sx,sy,sz,ex,ey,ez,x,y,z;
char cube[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int dir[6][3] = {1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1}; struct Node
{
int x,y,z,t;
Node(int i,int j,int m,int n):x(i),y(j),z(m),t(n){} //构造
Node(){}
}pre; bool judge(int i, int j, int k) //边界
{
if(i < 0 || i >= x ||j < 0 || j >= y || k < 0 || k >= z)
return false;
return true;
} void bfs()
{
memset(vis,0,sizeof(vis));
queue <Node> que;
que.push(Node(sx,sy,sz,0));
vis[sx][sy][sz] = 1;
while(!que.empty())
{
pre = que.front(); que.pop();
if(pre.x == ex && pre.y == ey && pre.z == ez)
{
printf("Escaped in %d minute(s).\n", pre.t);
return ;
}
for(int i = 0; i < 6; i++)
{
int xx = pre.x + dir[i][0];
int yy = pre.y + dir[i][1];
int zz = pre.z + dir[i][2];
if(!vis[xx][yy][zz] && judge(xx,yy,zz) && cube[xx][yy][zz] != '#')
{
vis[xx][yy][zz] = 1;
que.push(Node(xx,yy,zz,pre.t+1));
}
}
}
printf("Trapped!\n");
} int main()
{
while(scanf("%d %d %d", &x, &y, &z) != EOF)
{
if(!x && !y && !z) break;
for(int i = 0; i < x; i++)
for(int j = 0; j < y; j++)
scanf("%s", cube[i][j]);
for(int i = 0; i < x; i++)
for(int j = 0; j < y; j++)
for(int k = 0; k < z; k++)
if(cube[i][j][k] == 'S')
sx = i,sy = j,sz = k;
else if(cube[i][j][k] == 'E')
ex = i,ey = j,ez = k;
bfs();
}
return 0;
}