天天看點

[POJ](2251)Dungeon Master ---三維bfs

Dungeon Master

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 37721 Accepted: 14415

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!
      

解題新知:這是一個三維的bfs題,就是在建立搜尋模型的時候将它建成三維的就可以。

AC代碼:

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
typedef struct Node
{
    int x;
    int y;
    int z;
    int step;
}node;
node start,now,next;
int dir[6][3] = {{0,0,-1},{0,0,1},{0,1,0},{0,-1,0},{-1,0,0},{1,0,0}};
char mmap[35][35][35];
int vis[35][35][35];
int ans;
int xx;
int yy;
int zz;
queue<node>q;
bool check()
{
    if(mmap[next.x][next.y][next.z]!='#'&&next.x>=0 && next.x<xx && next.y>=0 && next.y <yy && next.z>=0 && next.z<zz && !vis[next.x][next.y][next.z])
        return true;
    else
        return false;
}
void bfs()
{
    q.push(start);
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        if(mmap[now.x][now.y][now.z] == 'E')
        {
            ans = now.step;
            return;
        }
        for(int i = 0;i<6;i++)
        {
            next.x = now.x+dir[i][0];
            next.y = now.y+dir[i][1];
            next.z = now.z+dir[i][2];
            if(check())
            {
                vis[next.x][next.y][next.z] = 1;
                next.step = now.step+1;
                q.push(next);
            }
        }
    }
}
int main()
{
    while(~scanf("%d %d %d",&xx,&yy,&zz)&&xx&&yy&&zz)
    {
        while(!q.empty())
            q.pop();
        ans = 0;
        memset(mmap,0,sizeof(mmap));
        memset(vis,0,sizeof(vis));
        for(int i=0;i<xx;i++)
        {
            for(int j=0;j<yy;j++)
            {
                scanf("%s",mmap[i][j]);
                for(int k=0;k<zz;k++)
                {
                    if(mmap[i][j][k] == 'S')
                    {
                        start.x = i;
                        start.y = j;
                        start.z = k;
                        start.step = 0;
                    }
                }
            }
        }
        bfs();
        if(ans!=0)
            printf("Escaped in %d minute(s).\n",ans);
        else
            printf("Trapped!\n");
    }
    return 0;
}