Dungeon Master BFS

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bengshakalakaka/article/details/79438274
Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 42008   Accepted: 15911

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<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#include<cmath>
using namespace std;
#define MAXN 50
char tu[MAXN][MAXN][MAXN];
int vis[MAXN][MAXN][MAXN];
struct stu
{
    int a,b,c;
    int coun;
};
queue<stu>mm;
int l,r,c;
int mov[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
int BFS(int u,int v,int w)
{
    queue<stu>hh;
    stu HA;
    stu tmp;//当前位置
    stu zz;//过渡
    HA.coun=0;
    HA.a=u;
    HA.b=v;
    HA.c=w;
    vis[u][v][w]=1;
    hh.push(HA);
    while(!hh.empty())
    {
        tmp=hh.front();
        if(tu[tmp.a][tmp.b][tmp.c]=='E')
        {
            return tmp.coun;
        }
        hh.pop();
        for(int i=0; i<6; i++)
        {


                u=tmp.a+mov[i][0];
                v=tmp.b+mov[i][1];
                w=tmp.c+mov[i][2];

            if((!vis[u][v][w])&&((tu[u][v][w]=='.')||(tu[u][v][w]=='E'))&&(u<=l)&&(u>=0)&&(v>=0)&&(v<=r)&&(w<=c)&&(w>=0))
            {
                zz.a=u;
                zz.b=v;
                zz.c=w;
                zz.coun=tmp.coun+1;
                hh.push(zz);
                vis[u][v][w]++;
            }

        }
    }
    return -1;
}
int main()
{
    int u,v,w;
    int result;
    while(scanf("%d%d%d",&l,&r,&c)&&(l||r||c))
    {
        memset(tu,0,sizeof(tu));
        memset(vis,0,sizeof(vis));
        for(int i=0; i<l; i++)
        {
            for(int j=0; j<r; j++)
            {
                for(int k=0; k<c; k++)
                {
                    cin>>tu[i][j][k];
                    if(tu[i][j][k]=='S')
                    {
                        u=i;
                        v=j;
                        w=k;
                    }
                }
            }
        }
        result=BFS(u,v,w);
        if(result>=0)
        {
            printf("Escaped in %d minute(s).\n",result);
        }
        else
        {
            printf("Trapped!\n");
        }
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/bengshakalakaka/article/details/79438274