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?
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!
题目大意:就是给你一个3D的迷宫,比平时的迷宫,多了个层数,也就是说,你可以上楼或者下楼,如果那条路可以走的话。这道题目就是从字母S开始出发 如果走到了E 那么就说明到达终点,然后输出格式,按照题目要求就没什么问题。这个基本也可以算是模板题目了,只是二维数组变成了三维数组而已
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
using namespace std;
int vis[35][35][35]; //访问标记
int dir[4][2] = {0,1,0,-1,1,0,-1,0}; //在平面上移动
int dir1[2] = {1,-1}; //上下楼
char map[35][35][35]; //地图
int l,r,c; //分别代表有几层楼,每一层有几行,有几列
int res ; //记录最后的结果
struct sta
{
int x,y,z;
char c;
int count; //走的步数
}num[100000];
int check(int x,int y,int z) //判断能不能走这个点
{
if(vis[z][y][x] == 0 && x >=0 && x < c && y >= 0 && y < r && z >=0 && z < l && map[z][y][x] != '#')
return 1;
else
return 0;
}
void bfs(sta s) //这个按照基本模板来就好了
{
queue <sta> q;
sta now,next;
q.push(s);
s.count = 0;
vis[s.z][s.y][s.x] = 1;
while (!q.empty())
{
now = q.front();
q.pop();
if (now.c == 'E') //到达终点。记录步数,退出
{
res = now.count;
return ;
}
for (int i = 0 ; i < 2 ; i++ ) //上下楼
{
next.z = now.z + dir1[i];
next.x = now.x;
next.y = now.y;
next.c = map[next.z][next.y][next.x];
next.count = now.count + 1;
if ( check(next.x,next.y,next.z))
{
vis[next.z][next.y][next.x] = 1;
q.push(next);
}
}
for (int i = 0 ; i < 4 ; i++ ) //一个平面上的上下左右
{
next.x = now.x + dir[i][0];
next.y = now.y + dir[i][1];
next.z = now.z;
next.c = map[next.z][next.y][next.x];
next.count = now.count +1;
if ( check(next.x,next.y,next.z))
{
vis[next.z][next.y][next.x] = 1; //做好走过的标记
q.push(next);
}
}
}
return ;
}
int main()
{
while (~scanf("%d%d%d",&l,&r,&c))
{
if ( l == 0 && r == 0 && c == 0)
{
break;
}
getchar();
int x = 0; //结构数组的下标
int v = 0; //开始点的下标
res = 0; //初始化结果
memset(vis,0,sizeof(vis)); //初始化访问标记
for (int i = 0 ; i < l ; i++ )
{
for (int j = 0 ; j < r ; j ++ )
{
for (int k = 0 ; k < c ; k ++ )
{
scanf("%c",&map[i][j][k]);
num[x].x = k;
num[x].y = j;
num[x].z = i;
num[x].c = map[i][j][k];
if (map[i][j][k] == 'S') //记录开始点
{
v = x;
}
x++;
}
getchar();
}
if ( i != l-1)
getchar();
}
bfs(num[v]); //从开始点bfs
if ( res > 0 ) //如果可以走到目的地
{
printf("Escaped in %d minute(s).\n",res);
}
else //如果不可以走到目的地
{
printf("Trapped!\n");
}
}
return 0;
}