您的位置:首页 > 其它

POJ - 2251 Dungeon Master BFS

2017-04-24 21:08 459 查看
Problem 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<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std;
char s[35][35][35];
int vis[35][35][35];
int x[8] = {0, 0, 0, 0, 1, -1};
int y[8] = {-1, 0, 1, 0, 0, 0};//左下右上 上一层,下一层
int z[8] = {0, 1, 0, -1, 0, 0};
int l, r, c, top;
struct node
{
int x2, y2, z2;
};
queue<node> q;
int judge(int x1, int y1, int z1)
{
if(x1 >= 0 && x1 < l && y1 >= 0 && y1 < r && z1 >= 0 && z1 < c && !vis[x1][y1][z1] && s[x1][y1][z1] == '.')
{
return 1;
}
else if(x1 >= 0 && x1 < l && y1 >= 0 && y1 < r && z1 >= 0 && z1 < c && !vis[x1][y1][z1] && s[x1][y1][z1] == 'E')
return 1;
else return 0;
}
void bfs(int x1, int y1, int z1)//层数,行,列
{
int i;
node tem, t;
tem.x2 = x1; tem.y2 = y1; tem.z2 = z1;
vis[x1][y1][z1] = 1;
q.push(tem);//入队列
while(!q.empty())//不为空
{
tem = q.front();
q.pop();
if(s[tem.x2][tem.y2][tem.z2] == 'E')//能出去
{
printf("Escaped in %d minute(s).\n", vis[tem.x2][tem.y2][tem.z2] - 1);//花费的时间
top = 1;
while(!q.empty())//清空
q.pop();
break;
}
for(i = 0; i < 6; i++)
{
if(judge(tem.x2 + x[i], tem.y2 + y[i], tem.z2 + z[i]))//判断能否走
{
vis[tem.x2 + x[i]][tem.y2 + y[i]][tem.z2 + z[i]] = vis[tem.x2][tem.y2][tem.z2] + 1;//更新花费的时间
t.x2 = tem.x2 + x[i]; t.y2 = tem.y2 + y[i]; t.z2 = tem.z2 + z[i];
q.push(t);//入队列
}
}
}
}
int main()
{
int i, j, k, top1;
while(~scanf("%d %d %d", &l, &r, &c))
{
if(!l && !r && !c) break;
top = 0; top1 = 0;
memset(vis, 0, sizeof(vis));
for(i = 0; i < l; i++)
{
for(j = 0; j < r; j++)
{
scanf("%s", s[i][j]);
}
}
for(i = 0; i < l; i++)
{
for(j = 0; j < r; j++)
{
for(k = 0; k < c; k++)
{
if(s[i][j][k] == 'S')
{
bfs(i, j, k);

4000
top1 = 1;
if(!top) printf("Trapped!\n");
break;
}
}
if(top1) break;
}
if(top1) break;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj bfs