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!
C++
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN = 30 + 5;
char maze[MAXN][MAXN][MAXN];
bool 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};
int n, m, k;
int sx, sy, sz, ex, ey, ez;
struct node
{
int x, y, z, step;
};
bool judge(int x, int y, int z)
{
if(x < 0 || y < 0 || z < 0 || x >= n || y >= m || z >= k || maze[x][y][z] == '#')
return false;
return true;
}
int bfs()
{
queue<node> q;
node now, next;
now.x = sx;
now.y = sy;
now.z = sz;
now.step = 0;
vis[sx][sy][sz] = true;
q.push(now);
while(!q.empty())
{
now = q.front();
q.pop();
if(now.x == ex && now.y == ey && now.z == ez){
return now.step;
}
for(int i = 0; i < 6; i ++){
next.x = now.x + dir[i][0];
next.y = now.y + dir[i][1];
next.z = now.z + dir[i][2];
if(judge(next.x, next.y, next.z) && !vis[next.x][next.y][next.z]){
vis[next.x][next.y][next.z] = true;
next.step = now.step + 1;
q.push(next);
}
}
}
return 0;
}
int main()
{
while(scanf("%d%d%d", &n, &m, &k) != EOF)
{
if(n == 0 && m == 0 && k == 0) break;
for(int i = 0; i < n; i ++){
for(int j = 0; j < m; j ++){
scanf("%s", maze[i][j]);
for(int t = 0; t < k; t ++){
if(maze[i][j][t] == 'S'){
sx = i; sy = j; sz = t;
}
else if(maze[i][j][t] == 'E'){
ex = i; ey = j; ez = t;
}
}
}
}
memset(vis, false, sizeof(vis));
int ans = bfs();
if(ans) printf("Escaped in %d minute(s).\n", ans);
else printf("Trapped!\n");
}
return 0;
}
迷宫逃脱算法
本文介绍了一个3D迷宫逃脱问题的解决算法,使用广度优先搜索(BFS)找到从起点到终点的最短路径。迷宫由单位立方体构成,可能充满岩石或为空,玩家需在一分钟内向六个方向移动以寻找出口。
466

被折叠的 条评论
为什么被折叠?



