POJ 2251 Dungeon Master(3D迷宫 bfs)
Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 28416 | Accepted: 11109 |
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迷宫,给你起点终点,找出最短路径。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | #include<iostream> #include<cstdio> #include<queue> #include<cstring> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 35; char maze[maxn][maxn][maxn]; int dis[maxn][maxn][maxn]; int L, R, C; struct Node { int z, x, y; Node( int zz, int xx, int yy): z(zz), x(xx), y(yy) {} }; int bfs( int sz, int sx, int sy, int gz, int gx, int gy) { queue<Node>que; int dx[5] = { -1, 0, 1,0}, dy[5] = {0, -1, 0, 1}, dz[3] = {0, -1, 1}; memset (dis, INF, sizeof (dis)); dis[sz][sx][sy] = 0; que.push(Node(sz, sx, sy)); while (!que.empty()) { Node pos = que.front(); que.pop(); if (pos.z == gz && pos.x == gx && pos.y == gy) { break ; } for ( int i = 0; i < 3; i++) { if (i) { int nz = pos.z + dz[i], nx = pos.x, ny = pos.y; if (nz >= 0 && nz < L && nx >= 0 && nx < R && ny >= 0 && ny < C && maze[nz][nx][ny] != '#' && dis[nz][nx][ny] == INF) { que.push(Node(nz, nx, ny)); dis[nz][nx][ny] = dis[pos.z][pos.x][pos.y] + 1; } } else { for ( int j = 0; j < 5; j++) { int nz = pos.z + dz[i], nx = pos.x + dx[j], ny = pos.y + dy[j]; if (nz >= 0 && nz < L && nx >= 0 && nx < R && ny >= 0 && ny < C && maze[nz][nx][ny] != '#' && dis[nz][nx][ny] == INF) { que.push(Node(nz, nx, ny)); dis[nz][nx][ny] = dis[pos.z][pos.x][pos.y] + 1; } } } } } return dis[gz][gx][gy]; } int main() { //freopen("input.txt", "r", stdin); while (~ scanf ( "%d%d%d" , &L, &R, &C) && L && R && C) { int sz, sx, sy, gz, gx, gy; for ( int i = 0; i < L; i++) { for ( int j = 0; j < R; j++) { scanf ( "%s" , maze[i][j]); for ( int k = 0; k < C; k++) { if (maze[i][j][k] == 'S' ) { sz = i, sx = j, sy = k; } if (maze[i][j][k] == 'E' ) { gz = i, gx = j, gy = k; } } } getchar (); } bfs(sz, sx, sy, gz, gx, gy) == INF ? printf ( "Trapped!\n" ) : printf ( "Escaped in %d minute(s).\n" , dis[gz][gx][gy]); } return 0; } |
┆ 凉 ┆ 暖 ┆ 降 ┆ 等 ┆ 幸 ┆ 我 ┆ 我 ┆ 里 ┆ 将 ┆ ┆ 可 ┆ 有 ┆ 谦 ┆ 戮 ┆ 那 ┆ ┆ 大 ┆ ┆ 始 ┆ 然 ┆
┆ 薄 ┆ 一 ┆ 临 ┆ 你 ┆ 的 ┆ 还 ┆ 没 ┆ ┆ 来 ┆ ┆ 是 ┆ 来 ┆ 逊 ┆ 没 ┆ 些 ┆ ┆ 雁 ┆ ┆ 终 ┆ 而 ┆
┆ ┆ 暖 ┆ ┆ 如 ┆ 地 ┆ 站 ┆ 有 ┆ ┆ 也 ┆ ┆ 我 ┆ ┆ 的 ┆ 有 ┆ 精 ┆ ┆ 也 ┆ ┆ 没 ┆ 你 ┆
┆ ┆ 这 ┆ ┆ 试 ┆ 方 ┆ 在 ┆ 逃 ┆ ┆ 会 ┆ ┆ 在 ┆ ┆ 清 ┆ 来 ┆ 准 ┆ ┆ 没 ┆ ┆ 有 ┆ 没 ┆
┆ ┆ 生 ┆ ┆ 探 ┆ ┆ 最 ┆ 避 ┆ ┆ 在 ┆ ┆ 这 ┆ ┆ 晨 ┆ ┆ 的 ┆ ┆ 有 ┆ ┆ 来 ┆ 有 ┆
┆ ┆ 之 ┆ ┆ 般 ┆ ┆ 不 ┆ ┆ ┆ 这 ┆ ┆ 里 ┆ ┆ 没 ┆ ┆ 杀 ┆ ┆ 来 ┆ ┆ ┆ 来 ┆
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)