链接:http://poj.org/problem?id=2251
题目:
Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15531 | Accepted: 6008 |
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!
解题思路:
这是迷宫求解类的问题,一种典型的搜索类型的题目。我们可以用bfs()搜索走迷宫的最短步数,用方向向量标记走的六个方向。
代码:
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int MAXN = 32;
char map[MAXN][MAXN][MAXN];
int vis[MAXN][MAXN][MAXN];
int L, R, C, sx, sy, sz;
int dz[] = { 0, 0, 0, 0, -1, 1};
int dx[] = {-1, 1, 0, 0, 0, 0};
int dy[] = { 0, 0, -1, 1, 0, 0};
struct Point
{
int z, x, y, cur;
};
int bfs()
{
queue<Point> q;
Point p; p.cur = 0;
p.z = sz; p.x = sx; p.y = sy;
q.push(p);
while(!q.empty())
{
Point pp;
pp = q.front();
q.pop();
if('E' == map[pp.z][pp.x][pp.y]) return pp.cur;
if(vis[pp.z][pp.x][pp.y]) continue;
vis[pp.z][pp.x][pp.y] = 1;
Point tmp;
for(int i = 0; i < 6; i++)
{
int z = pp.z + dz[i];
int x = pp.x + dx[i];
int y = pp.y + dy[i];
if(!vis[z][x][y] && z >= 0 && z < L &&
x >= 0 && x < R && y >= 0 && y < C &&
'.' == map[z][x][y] || 'E' == map[z][x][y])
{
tmp.z = z;
tmp.x = x;
tmp.y = y;
tmp.cur = pp.cur + 1;
// printf("%d %d %d cur = %d\n", tmp.z, tmp.x, tmp.y, tmp.cur);
q.push(tmp);
}
}
}
return 0;
}
int main()
{
while(~scanf("%d%d%d", &L, &R, &C) && (L || R || C))
{
getchar();
memset(map, 0, sizeof(map));
memset(vis, 0, sizeof(vis));
for(int k = 0; k < L; k++)
{
for(int i = 0; i < R; i++)
{
for(int j = 0; j < C; j++)
{
scanf("%c", &map[k][i][j]);
if('S' == map[k][i][j])
{
sz = k;
sx = i;
sy = j;
}
}
getchar();
}
getchar();
}
int ans = bfs();
if(ans)
{
printf("Escaped in %d minute(s).\n", ans);
}
else
{
puts("Trapped!");
}
}
return 0;
}