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

B - Dungeon Master

  POJ - 2251 

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 <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
using namespace std;
int l,n,m;
int x1,y1,z1;
int s[6][3]={{0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}}; //东南西北上下六个方向
char a[31][31][31]; 
int t[31][31][31]; //记录走到该点的最小时间
queue <int> q;
int work(int x,int y,int z)
{
    if(x<=0||x>l||y<=0||y>n||z<=0||z>m) return 0; //越界返0
    if(a[x][y][z]=='#'||t[x][y][z]>=0) return 0; //不能走或者该点已被走过返0
    return 1;
}
int main()
{
    while(~scanf("%d%d%d",&l,&n,&m))
    {
        if(l==0&&n==0&&m==0) break;   
        memset(t,-1,sizeof(t));  //初始化各点最小时间
        for(int i=1;i<=l;i++)
        {
            for(int j=1;j<=n;j++)
            {
                for(int k=1;k<=m;k++)
                {
                    cin>>a[i][j][k];
                    if(a[i][j][k]=='S')
                    {
                        q.push(i);
                        q.push(j);
                        q.push(k);
                        t[i][j][k]=0;    //起点时间记为0
                    }
                    if(a[i][j][k]=='E')
                    {
                        x1=i;
                        y1=j;
                        z1=k;
                    }
                }
            }
        }
        while(!q.empty())
        {
            int x=q.front();q.pop();
            int y=q.front();q.pop();
            int z=q.front();q.pop();
            for(int i=0;i<6;i++)
            {
                int xx=x+s[i][0];
                int yy=y+s[i][1];
                int zz=z+s[i][2];
                if(work(xx,yy,zz)) //如果可以走
                {
                    t[xx][yy][zz]=t[x][y][z]+1; //该点就是上一个点的时间+1
                    q.push(xx);
                    q.push(yy);
                    q.push(zz);
                }
            }
        }
        if(t[x1][y1][z1]!=-1) printf("Escaped in %d minute(s).\n",t[x1][y1][z1]); //如果终点不为-1,就是有走过
        else printf("Trapped!\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41037114/article/details/80849475