POJ 2251 Dungeon Master
题目链接:https://vjudge.net/problem/POJ-2251
题目描述:
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!
这道题类似二维迷宫找最短路,BFS层序遍历模板。一开始先找到起始坐标s。然后遍历六个方向north, south, east, west, up or down,使用vis数组来标记是否访问过。
坑:
1.开始找s的时候,三重for循环里if break只能跳出最近一层for循环。
2.可以通过queue<Node>
或 c++ shared_ptr<Node> p = make_shared<Node>(a, b, c)
与queue<shared_ptr<Node>>
来解决Node的自动释放。
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 35;
char maze[maxn][maxn][maxn];
bool vis[maxn][maxn][maxn];
int L, R, C;
int ans;
string blank_line;
struct Node {
int _x, _y, _z;
Node(int x, int y, int z):_x(x), _y(y), _z(z) {}
};
bool bfs() {
bool flag = true;
static int dx[] = {0, 0, 1, -1, 0, 0};
static int dy[] = {1, -1, 0, 0, 0, 0};
static int dz[] = {0, 0, 0, 0, 1, -1};
int a, b, c;
Node s(0, 0, 0);
for (a = 0; a < L; a ++)
for (b = 0; b < R; b ++)
for (c = 0; c < C; c ++)
if (maze[a][b][c] == 'S') s = Node(a, b, c);
queue<Node> q;
q.push(s);
while (!q.empty()) {
int size = q.size();
for (int i = 0; i < size; i ++) {
Node it = q.front();
for (int i = 0; i < 6; i ++) {
int new_x = it._x + dx[i];
int new_y = it._y + dy[i];
int new_z = it._z + dz[i];
if (0 <= new_x && new_x < L && 0 <= new_y && new_y < R && 0 <= new_z
&& new_z < C && !vis[new_x][new_y][new_z] && maze[new_x][new_y][new_z] != '#') {
if (maze[new_x][new_y][new_z] == 'E') return flag;
if (maze[new_x][new_y][new_z] == '.') {
Node n(new_x, new_y, new_z);
vis[new_x][new_y][new_z] = true;
q.push(n);
}
}
}
q.pop();
}
ans ++;
}
flag = false;
return flag;
}
void init() {
ans = 0;
memset(maze, 0, sizeof(maze));
memset(vis, 0, sizeof(vis));
for (int i = 0; i < L; i ++)
for (int j = 0; j < R; j ++) {
cin >> maze[i][j];
}
getline(cin, blank_line); // skip blank line
}
int main() {
while (cin >> L >> R >> C) {
if (L == 0 && R == 0 && C == 0) break;
init(); // read input
bool flag = bfs();
if (flag) cout << "Escaped in " << ans + 1 <<" minute(s)." << endl;
else
cout << "Trapped!" << endl;
}
return 0;
}