题目链接:http://poj.org/problem?id=2251
Dungeon Master
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 37296 | Accepted: 14266 |
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!
Source
题解:
三维的纯BFS。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+7;
const int MAXN = 30+10;
struct node
{
int x, y, z, step;
};
int L, R, C;
char m[MAXN][MAXN][MAXN];
int vis[MAXN][MAXN][MAXN];
int dir[6][3] = { {1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1} };
queue<node>que;
int bfs(node s, node e)
{
ms(vis,0);
while(!que.empty()) que.pop();
s.step = 0;
vis[s.x][s.y][s.z] = 1;
que.push(s);
while(!que.empty())
{
node now = que.front();
que.pop();
if(now.x==e.x && now.y==e.y && now.z==e.z)
return now.step;
for(int i = 0; i<6; i++)
{
node tmp;
tmp.x = now.x + dir[i][0];
tmp.y = now.y + dir[i][1];
tmp.z = now.z + dir[i][2];
if(tmp.x>=1 && tmp.x<=L && tmp.y>=1 && tmp.y<=R && tmp.z>=1 && tmp.z<=C
&& m[tmp.x][tmp.y][tmp.z]!='#' && !vis[tmp.x][tmp.y][tmp.z] )
{
vis[tmp.x][tmp.y][tmp.z] = 1;
tmp.step = now.step + 1;
que.push(tmp);
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d%d",&L, &R, &C) && (L||R||C))
{
for(int i = 1; i<=L; i++)
for(int j = 1; j<=R; j++)
scanf("%s", m[i][j]+1);
node s, e;
for(int i = 1; i<=L; i++)
for(int j = 1; j<=R; j++)
for(int k = 1; k<=C; k++)
{
if(m[i][j][k]=='S') s.x = i, s.y = j, s.z = k;
if(m[i][j][k]=='E') e.x = i, e.y = j, e.z = k;
}
int ans = bfs(s,e);
if(ans==-1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n", ans);
}
return 0;
}
本文介绍了一个三维迷宫逃脱问题,通过BFS(宽度优先搜索)算法寻找从起点到终点的最短路径。文章提供了完整的C++代码实现,并详细解释了如何遍历三维空间中的每一个位置。

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



