[kuangbin带你飞]专题一 简单搜索 - B - Dungeon Master

B - Dungeon Master
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  POJ 2251

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
code:
#include <cstdio>
#include <queue>
#include <iostream>
#include <cstring>

using namespace std;
typedef long long ll;
typedef struct{
    int x,y,z,wal;
}data;
data star;
int n,k,ex,ey,ez,lev,hang,lie;
char maze[35][35][35];
int vis[35][35][35];
int dir[6][3]={ {1,0,0} , {0,1,0} , {0,0,1} , {0,-1,0} , {0,0,-1} , {-1,0,0} };

int bfs()
{
    queue <data> q;
    q.push(star);
    
    while(q.size())
    {
        data now=q.front();
        q.pop();
        
        if(now.x==ex&&now.y==ey&&now.z==ez){
            return now.wal;
        }
        data nex;
        nex.wal=now.wal+1;
        for(int i=0;i<6;i++)
        {
            int a=now.x+dir[i][0],b=now.y+dir[i][1],c=now.z+dir[i][2];
            
            if(vis[a][b][c]==0)
                if( 1 <= a && a <= lev && 1 <= b && b <= hang && 1 <= c && c <= lie )
                    if( maze[a][b][c] == '.' || maze[a][b][c] == 'E' )
                    {
                        vis[a][b][c]=1;
                        nex.x=a,nex.y=b,nex.z=c;
                        q.push(nex);
                    }
        }
    }
    return 0;
}

int main(void)
{
    
    while(cin>>lev>>hang>>lie&&lev+hang+lie)
    {
        memset(vis,0,sizeof vis);
        
        for(int i=1;i<=lev;i++)
            for(int j=1;j<=hang;j++)
                scanf("%s",maze[i][j]+1);
        
        for(int i=1;i<=lev;i++)
            for(int j=1;j<=hang;j++)
                for(int q=1;q<=lie;q++)
                {
                    if( maze[i][j][q]=='S' ) {
                        star.x=i,star.y=j,star.z=q;
                        vis[i][j][q]=1;
                        star.wal=0;
                    }
                    if( maze[i][j][q]=='E' ) {
                        ex=i,ey=j,ez=q;
                    }
                }
        int ans=bfs();

        if ( ans ) printf("Escaped in %d minute(s).\n",ans);
        else printf("Trapped!\n");
    }
}

你可能感兴趣的:(专题简单搜索)