[UVA532] 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 file 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#

Copy
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0

Sample Output#

Copy
Escaped in 11 minute(s). Trapped!

Solution#

Different to traditional maze puzzles, it's a 3-dimensional maze game.

But actually they are the same. The only thing we have to do is turn 4-direction BFS into 6-direction one.

Implementation#

Originlly, we have two array named dx and dy:

Copy
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; // (dx[0], dy[0]) => right // (dx[1], dy[1]) => forward // (dx[2], dy[2]) => left // (dx[3], dy[3]) => backward

When it comes to 3 dimensions, we have one dimension(z) and two direction(up&down) more.

Copy
const int dz[6] = {1, 0, 0, -1, 0, 0}, dx[6] = {0, 1, 0, 0, -1, 0}, dy[6] = {0, 0, 1, 0, 0, -1}; // (dx[0], dy[0], dz[0]) => up // (dx[1], dy[1], dz[1]) => right // (dx[2], dy[2], dz[2]) => forward // (dx[3], dy[3], dz[3]) => down // (dx[4], dy[4], dz[4]) => left // (dx[5], dy[5], dz[5]) => backward

That's the core of this algorithm.

Chore#

Input is terminated by three zeroes for L, R and C.

In fact, most problems on UVa are like this. You have to write a while(true) loop, read inputs at the beginning of the loop, and break until you've got an invalid input.

Code#

Copy
#include <bits/stdc++.h> using namespace std; int l, n, m; struct pos { int z, x, y, step; pos(int z, int x, int y, int step) : z(z), x(x), y(y), step(step) {} }; int sz, sx, sy; int tz, tx, ty; bool mat[31][31][31]; bool vis[31][31][31]; const int dz[6] = {1, 0, 0, -1, 0, 0}, dx[6] = {0, 1, 0, 0, -1, 0}, dy[6] = {0, 0, 1, 0, 0, -1}; bool pan(int z, int x, int y) { return z >= 1 && z <= l && x >= 1 && x <= n && y >= 1 && y <= m && mat[z][x][y] == 0; } int main() { while (true) { cin >> l >> n >> m; if (l == 0 && n == 0 && m == 0) break; for (int k = 1; k <= l; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { char c; cin >> c; if (c == '.') mat[k][i][j] = 0; else if (c == '#') mat[k][i][j] = 1; else if (c == 'S') { mat[k][i][j] = 0; sz = k; sx = i; sy = j; } else if (c == 'E') { mat[k][i][j] = 0; tz = k; tx = i; ty = j; } else { cin >> c; } vis[k][i][j] = 0; } } } queue<pos> q; q.push(pos(sz, sx, sy, 0)); vis[sz][sx][sy] = 1; bool flag = 1; while (!q.empty()) { pos head = q.front(); q.pop(); if (head.z == tz && head.x == tx && head.y == ty) { cout << "Escaped in " << head.step << " minute(s).\n"; flag = 0; break; } for (int i = 0; i < 6; i++) { int zz = head.z + dz[i], xx = head.x + dx[i], yy = head.y + dy[i]; if (!pan(zz, xx, yy)) continue; if (vis[zz][xx][yy]) continue; q.push(pos(zz, xx, yy, head.step + 1)); vis[zz][xx][yy] = 1; } } if (flag) cout << "Trapped!\n"; } }
posted @   water_lift  阅读(201)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示
CONTENTS