您的位置:首页 > 其它

POJ 2251 Dungeon Master

2013-08-26 11:07 399 查看
这道题就是给你一个三维的坐标求S到E的最短路,如果有就输出距离没有就输出Trapped!。

就是一个BFS寻找最短路径、、和HDU的1429胜利大逃亡基本上一样、、

Dungeon Master

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14210 Accepted: 5519
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 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!

#include <stdio.h>
#include <string.h>
#include <queue>
#include <stdlib.h>
#include <iostream>

using namespace std;

int map[35][35][35], sum;
int v[35][35][35];
int  n, m, t;
int x2, y2, z2, x1, y1, z1;
int to[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
struct node
{
int x, y, z;
int step;
} f[100005];
int charg(int a, int b, int c)
{
if(a>=0 && a<n && b>=0 && b<m && c>=0 && c<t)
return 1;
else
return 0;
}

int bfs()
{
struct node p;
int l = 0, r = 0;
f[l].x = x1;
f[l].y = y1;
f[l].z = z1;
f[l].step = 0;
v[x1][y1][z1] = 1;
r++;
while(l < r)
{
for(int i = 0; i < 6; i++)
{
p.x = f[l].x+to[i][0], p.y = f[l].y+to[i][1], p.z = f[l].z+to[i][2];
if(charg(p.x, p.y, p.z))
if(map[p.x][p.y][p.z] && !v[p.x][p.y][p.z])
{
v[p.x][p.y][p.z] = 1;
p.step = f[l].step+1;
if(p.x == x2 && p.y == y2 && p.z == z2)
{
sum = p.step;
return 1;
}
else
f[r++] = p;
}
}
l++;
}
return 0;
}
int main()
{
int i, j, k;
char s[101];
while(~scanf("%d %d %d", &n, &m, &t) &&(n||m||t))
{
memset(v , 0 , sizeof(v));
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
{
scanf("%s",s);
for(k = 0; k < t; k++)
{
if(s[k] == 'S')
{
x1 = i, y1 = j, z1 = k;
map[i][j][k] = 1;
}
else if(s[k] == 'E')
{
x2 = i, y2 = j, z2 = k;
map[i][j][k] = 1;
}
else if(s[k] == '#')
map[i][j][k] = 0;
else if(s[k] == '.')
map[i][j][k] = 1;
}
}
if(bfs())
printf("Escaped in %d minute(s).\n",sum);
else
printf("Trapped!\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM POJ