1. 程式人生 > >【POJ2251】Dungeon Master(三維bfs)

【POJ2251】Dungeon Master(三維bfs)

題目連結

Dungeon Master

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 48900 Accepted: 18463

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!

Source

【題意】

一個三維的迷宮,已知起點和終點,看是否能從起點走到終點,若可以則輸出最短路徑,若不可以則輸出Trapped!

【解題思路】

因為dfs會超時,因為bfs的自身優點只需找到第一個符合條件的值返回即是最短路徑。

另外這道題的字串處理也很噁心,需要細心一點。

【程式碼】

#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=35;
int dx[6]={1,0,0,0,0,-1};
int dy[6]={0,1,0,0,-1,0};
int dz[6]={0,0,1,-1,0,0};
char s[maxn][maxn][maxn];
int l,r,c,si,sj,sk,ei,ej,ek;
int vis[maxn][maxn][maxn];
struct Node
{
    int x,y,z,num;
};
int bfs()
{
    Node a;
    a.x=si;a.y=sj;a.z=sk;a.num=0;
    queue<Node>q;
    q.push(a);
    while(!q.empty())
    {
        Node t=q.front();
        q.pop();
        if(t.x==ei && t.y==ej && t.z==ek)return t.num;
        for(int i=0;i<6;i++)
        {
            Node a;
            a.x=t.x+dx[i];
            a.y=t.y+dy[i];
            a.z=t.z+dz[i];
            if(a.x>=0 && a.x<l && a.y>=0 && a.y<r && a.z>=0 && a.z<c && s[a.x][a.y][a.z]=='.' && !vis[a.x][a.y][a.z])
            {
                a.num=t.num+1;
                q.push(a);
                vis[a.x][a.y][a.z]=1;
            }
        }
    }
    return 0;
}
int main()
{
    while(~scanf("%d%d%d",&l,&r,&c) && l && r &&c)
    {
        memset(vis,0,sizeof(vis));
        getchar();
        for(int i=0;i<l;i++)
        {
            for(int j=0;j<r;j++)
            {
                for(int k=0;k<c;k++)
                {
                    char ch;
                    scanf("%c",&ch);
                    if(ch=='S')
                    {
                        s[i][j][k]='.';
                        si=i;
                        sj=j;
                        sk=k;
                    }
                    else if(ch=='E')
                    {
                        s[i][j][k]='.';
                        ei=i;
                        ej=j;
                        ek=k;
                    }
                    else if(ch=='.')s[i][j][k]='.';
                    else if(ch=='#')s[i][j][k]='#';
                }
                getchar();
            }
            if(i!=l-1)getchar();
        }
        int ans=bfs();
        if(ans)printf("Escaped in %d minute(s).\n",ans);
        else printf("Trapped!\n");
    }
    return 0;
}