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!题目大意:你被困在一个三维的迷宫里啦,迷宫里有很多墙和路,在迷宫里可以向上下左右前后6个方向走,只有当上一层对应位置也是路的时候才能向上走,你会出现在上面一层的对应位置,移动的时间均为1分钟,求从迷宫出来的最短时间。
解题思路:今天训练赛遇到了一个三维BFS问题,没有做出来。下午A了那道题以后想起这道类似的三维BFS,就写了一下,其实并没有很难,就是多了一个条件的判断。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
struct node
{
int x,y,z;
int time;
}r,s,t;
int L,R,C;
int sx,sy,sz;
int dir[6][3] = {{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}};
char maze[33][33][33];
int bfs()
{
r.x = sx,r.y = sy,r.z = sz,r.time = 0;
maze[r.x][r.y][r.z] = '#';
queue<node> que;
que.push(r);
while(que.size()){
s = que.front();
que.pop();
for(int i = 0;i < 6;i++){
t.x = s.x + dir[i][0];
t.y = s.y + dir[i][1];
t.z = s.z + dir[i][2];
t.time = s.time;
if(0 <= t.x && t.x < L && 0 <= t.y && t.y < R && 0 <= t.z && t.z < C && maze[t.x][t.y][t.z] != '#'){
t.time++;
if(maze[t.x][t.y][t.z] == 'E'){
return t.time;
}
que.push(t);
maze[t.x][t.y][t.z] = '#';
}
}
}
return 0;
}
int main()
{
while(scanf("%d %d %d",&L,&R,&C) != EOF && (L || R || C)){
for(int i = 0;i < L;i++){
for(int j = 0;j < R;j++){
scanf("%s",maze[i][j]);
}
}
for(int i = 0;i < L;i++){
for(int j = 0;j < R;j++){
for(int k = 0;k < C;k++){
if(maze[i][j][k] == 'S'){
sx = i;
sy = j;
sz = k;
}
}
}
}
int ans = bfs();
if(ans == 0)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans);
}
return 0;
}