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!

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

typedef struct node{
    int l, r, c;
    int num;
}dot;

int l, r, c;
int s_l, s_r, s_c, ans;
char ch[32][32][32];
bool step[32][32][32];
int dir[6][3] = { {0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0} };


void find_s()
{
    for(int i=0; i<l; i++){
        for(int j=0; j<r; j++){
            for(int k=0; k<c; k++){
                if( ch[i][j][k]=='S' ){ s_l=i; s_r=j; s_c=k; return;}
            }
        }
    }
}
bool judge( dot x )
{
    if( x.l<0||x.l>=l || x.r<0||x.r>=r || x.c<0||x.c>=c )    return false;
    if( step[x.l][x.r][x.c] )                                return false;
    if( ch[x.l][x.r][x.c]=='#' )                             return false;
    return true;
}
int bfs()
{
    dot now, next;
    queue<dot>Q;

    now.l=s_l; now.r=s_r; now.c=s_c; now.num = 0;
    if(!judge(now)) return -1;
    step[now.l][now.r][now.c]=true;
    Q.push(now);
    while( !Q.empty() ){
        now = Q.front();  Q.pop();
        for(int i=0; i<6; i++){
                next.l=now.l+dir[i][0]; next.r=now.r+dir[i][1]; next.c=now.c+dir[i][2]; next.num=now.num;
            if( judge(next) ){
                next.num++;
                if(ch[next.l][next.r][next.c]=='E')
                    return next.num;

                step[next.l][next.r][next.c] = true;
                Q.push(next);
            }
        }
    }
    return -1;
}

int main()
{
    while( scanf("%d%d%d", &l,&r,&c)!=-1 ){
        if( l+r+c==0 )     break;

        for(int i=0; i<l; i++){
            for(int j=0; j<r; j++){
                scanf("%s", ch[i][j]);
            }
            getchar();
        }
        memset(step, false, sizeof(step));
        s_l=-1; s_r=-1; s_c=-1;
        find_s();

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

    return 0;
}


你可能感兴趣的:(B - Dungeon Master)