Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
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!
翻译:
Description - 题目描述
[NWUACM]
你被困在一个三维的空间中,现在要寻找最短路径逃生!
空间由立方体单位构成
你每次向上下前后左右移动一个单位需要一分钟
你不能对角线移动并且四周封闭
是否存在逃出生天的可能性?如果存在,则需要多少时间?
Input - 输入
输入第一行是一个数表示空间的数量。
每个空间的描述的第一行为L,R和C(皆不超过30)。
L表示空间的高度。
R和C分别表示每层空间的行与列的大小。
随后L层地牢,每层R行,每行C个字符。
每个字符表示空间的一个单元。’#‘表示不可通过单元,’.‘表示空白单元。你的起始位置在’S’,出口为’E’。
每层空间后都有一个空行。L,R和C均为0时输入结束。
Output - 输出
每个空间对应一行输出。
如果可以逃生,则输出如下
Escaped in x minute(s).
x为最短脱离时间。
如果无法逃生,则输出如下
Trapped!
题意:在三维数组里BFS,
- 不要混淆x、y、z三坐标;
- 多组输入,每次要将队列 queue 和标记数组 vis[ ] 初始化为空和0;
- 输入的时候尽量使用 cin 读入,这样免去了使用 getchar 再吸收回车的麻烦,要使用 scanf 的话也可以,注意每一行输入完要吸收一次回车,每一层输入完吸收一次回车;
- 设置一个结构体,内包括三维坐标,记录 st, en, cur, nxt 三个状态。
- 巧妙地使用 vis 数组来记录步数,初始全部赋值为0, 每次选择一条路 vis + 1, 最后到达终点时的 vis 就是所走步数。
AC代码如下:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 32;
char mapp[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}};//2向右3向左4向上
int l, r, c;
struct node{
int ll, rr, cc;
char ch;
}st, en, cur, nxt;
queue <node > q;
bool check(int x, int y, int z){
if(x >= 0 && x < l && y >= 0 && y < r && z >= 0 && z < c && vis[x][y][z] == 0 && mapp[x][y][z] != '#') return 1;
return 0;
}
int bfs(){
q.push(st);
while(q.size()){
cur = q.front();
q.pop();
for(int i = 0; i < 6; i++){
nxt.ll = cur.ll + dir[i][0];
nxt.rr = cur.rr + dir[i][1];
nxt.cc = cur.cc + dir[i][2];
if(check(nxt.ll, nxt.rr, nxt.cc)){
vis[nxt.ll][nxt.rr][nxt.cc] = vis[cur.ll][cur.rr][cur.cc] + 1;
if(nxt.ll == en.ll && nxt.rr == en.rr && nxt.cc == en.cc)
return vis[nxt.ll][nxt.rr][nxt.cc];
q.push(nxt);
}
}
}
return -1;
}
int main(){
while(cin >> l >> r >> c){
while(q.size()) q.pop();
if(l == 0 && r == 0 && c == 0) return 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++){
cin >> mapp[i][j][k];
if(mapp[i][j][k] == 'S')
st.ll = i, st.rr = j, st.cc = k;
if(mapp[i][j][k] == 'E')
en.ll = i, en.rr = j, en.cc = k;
}
}
}
if( bfs() == -1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n", vis[nxt.ll][nxt.rr][nxt.cc]);
}
return 0;
}