Dungeon Master
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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!
三维空间上的搜索,和二维搜索一样,只要把每个方向别弄混淆了,相信都没什么问题,这里我就不多说了,直接附上代码
附上代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
char str[35][35][35];
int visit[35][35][35];
int dx[6] = {0,0,-1,1,0,0}, // 三维六个方向搜索 分别表示 x y z 的变化
dy[6] = {-1,1,0,0,0,0},
dz[6] = {0,0,0,0,-1,1};
int x2,y2,z2,X,Y,Z;
struct num
{
int x,y,z,step;
};
queue<num>Q;
int bfs(int x1,int y1,int z1)
{
num e = {x1,y1,z1,0};
Q.push(e);
visit[z1][y1][x1] = 1;
while(!Q.empty())
{
e = Q.front();
if(e.x == x2 && e.y == y2 && e.z == z2)
break;
Q.pop(); // 切记这个清空队列要在判断过是否为终点以后清除,因为有个能有可能队列的最后一个是符合条件的,如果先清除,那么在下面判断时时候会是队列为空,那么就会出现错误
for(int i = 0;i < 6;i++) // 对六个方向进行搜素
{
int sx = e.x + dx[i];
int sy = e.y + dy[i];
int sz = e.z + dz[i];
if(sx >= 0 && sx < X && sy >= 0 && sy < Y && sz >= 0 && sz < Z &&
!visit[sz][sy][sx] && (str[sz][sy][sx] == '.' || str[sz][sy][sx] == 'E')) // 是 ‘.’ 和 ‘E’且 没有搜索过的,进行进队列搜索
{
num e1 = {sx,sy,sz,e.step+1};
Q.push(e1);
visit[sz][sy][sx] = 1; // 走过以后标记为 1
}
}
}
if(Q.empty())
return -1;
while(!Q.empty()) // 搜索结束完以后进行队列清空操作
{
Q.pop();
}
return e.step;
}
int main()
{
int x1,y1,z1;
while(scanf("%d%d%d",&Z,&Y,&X) && (X != 0 || Y != 0 || Z != 0))
{
memset(visit,0,sizeof(visit));
for(int i = 0;i < Z;i++)
{
for(int j = 0;j < Y;j++)
{
scanf("%s",str[i][j]);
for(int k = 0;k < X;k++)
{
if(str[i][j][k] == 'S')
{
z1 = i;
y1 = j;
x1 = k;
}
if(str[i][j][k] == 'E')
{
z2 = i;
y2 = j;
x2 = k;
}
}
}
}
int sum = bfs(x1,y1,z1);
if(sum == -1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",sum);
}
return 0;
}