Dungeon Master
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! Source |
#include <string.h>
#include <queue>
#include <algorithm>
#include<stdio.h>
using namespace std;
char map[35][35][35];//代表输入的地图
int vis[35][35][35];//记录是否走过
int to[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};//空间六个面方向
int k,n,m;//面,行,列
int sx,sy,sz,ex,ey,ez;//起始位置和终止位置
struct node//代表坐标和走的步数
{
int x,y,z,step;
};
int check(int x,int y,int z)//检查是否符合题意
{
if(x<0||y<0||z<0||x>=k||y>=n||z>=m)//超出范围
return 1;
else if(map[x][y][z]=='#')//有岩石
return 1;
else if(vis[x][y][z])//走过该点
return 1;
else
return 0;//都没有的话返回0,代表可以走
}
int bfs()//搜索
{
node a,next;//建立两个结构体
queue<node>q;//建立队列
a.x=sx,a.y=sy,a.z=sz;//把起点坐标给到a结构体
a.step=0;//起点的步数为0
vis[sx][sy][sz]=1;//标记起点走过
q.push(a);//把起点放入队列
while(!q.empty())//当队列里不是空的时候
{
a=q.front();//拿出队列的首元素
q.pop();//清空内存
if(a.x==ex&&a.y==ey&&a.z==ez)//当a的位置和终点位置一样时返回步数
return a.step;
for(int i=0; i<6; i++)//改变方向
{
next=a;
next.x=a.x+to[i][0];
next.y = a.y+to[i][1];
next.z = a.z+to[i][2];
if(check(next.x,next.y,next.z))//检查是否符合要求
{
continue;
}
vis[next.x][next.y][next.z] = 1;//符合的话记录已经走过
next.step=a.step+1;//步数+1
q.push(next);//放入队列开始下一次循环
}
}
return 0;
}
int main()
{
int i,j,r;
while(~scanf("%d%d%d",&k,&n,&m)&&(n+k+m))
{
for(i=0; i<k; i++)//输入地图
{
for(j=0; j<n; j++)
{
scanf("%s",map[i][j]);
for(r=0; r<m; r++)
{
if(map[i][j][r]=='S')//标记起点
{
sx=i,sy=j,sz=r;
}
if(map[i][j][r]=='E')//标记终点
{
ex=i,ey=j,ez=r;
}
}
}
}
memset(vis,0,sizeof(vis));//先清空记录步数的数组表示还没开始走所以都是0
int ans=0;//走的步数
ans=bfs();
if(ans)
printf("Escaped in %d minute(s).\n",ans);//能走出去输出步数
else
printf("Trapped!\n");//走不出去
}
return 0;
}