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;
}


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