poj-2251 Dungeon Master BFS

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!

        题意:现在进行一个3D的游戏,S为起点E为终点,没移动一步需要1min,找到从S到E所需要的最短时间。

        思路:由题意这是一个三维的图,直接构建三维字符数组就行了,搜索的时候要用BFS,BFS可直接找到最短距离,而DFS遍历次数多,这道题如果不对DFS进行优化的话会超时。搜索的时候和二维图只有一点不同就是多了上下两个方向,其余一点不变。注意输入时两层for循环用%s输入,用%c个字符输入会导致图输入不全。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#define INF 0x3f3f3f3f

using namespace std;

struct node
{
    int x,y,z;
    int step;
}que,now,next;
int l,r,c,ans,flag;
int sx,sy,sz;
char build[50][50][50];
int vis[50][50][50];
int dx[]={1,-1,0,0,0,0};
int dy[]={0,0,1,-1,0,0};
int dz[]={0,0,0,0,1,-1};

void BFS()
{
    int x,y,z,i;
    queue<node>Q;
    vis[sz][sx][sy]=1;
    que.x=sx;
    que.y=sy;
    que.z=sz;
    que.step=0;
    Q.push(que);
    while(!Q.empty())
    {
        now=Q.front();
        Q.pop();
        for (i=0;i<6;i++)
        {
                x=next.x=now.x+dx[i];
                y=next.y=now.y+dy[i];
                z=next.z=now.z+dz[i];
                next.step=now.step+1;
                if(x<0||x>=r||y<0||y>=c||z<0||z>=l) continue;
                if(build[z][x][y]=='#') continue;
                if(build[z][x][y]=='E')
                {
                    ans=next.step;
                    flag=1;
                    return;
                }
                if(build[z][x][y]=='.'&&!vis[z][x][y])
                {
                    vis[z][x][y]=1;
                    Q.push(next);
                }
        }
    }
}
int main()
{
    int i,j,k;
    while(scanf("%d%d%d%*c",&l,&r,&c),l||r||c)
    {
        memset(vis,0,sizeof(vis));
        memset(build,0,sizeof(build));
        flag=0;
        for (i=0;i<l;i++)
        {
            for (j=0;j<r;j++)
            {
                scanf("%s",build[i][j]);
                for (k=0;k<c;k++)
                {
                    if(build[i][j][k]=='S')
                        sx=j,sy=k,sz=i;
                }
            }
        }
        BFS();
        if(flag)
            printf("Escaped in %d minute(s).\n",ans);
        else
            printf("Trapped!\n");
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章