Dungeon Master uva BFS

Dungeon Master 

You are trapped in a 3D dungeon and need to find the quickest way out!The dungeon is composedof unit cubes which may or may not be filled with rock. It takes one minuteto move one unit north,south, east, west, up or down. You cannot move diagonally and the maze issurrounded by solid rock on all sides.


Is an escape possible? If yes, how long will it take?

Input Specification 

The input file consists of a number of dungeons. Each dungeon descriptionstarts with a linecontaining 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 describesone 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 blankline after each level. Input is terminated by three zeroes for L, R and C.

Output Specification 

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

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char Map[33][33][33];
bool vis[33][33][33];
struct node
{
    int x,y;
    int f;
    int times;
};
int dir[6][3]={{0,1,0},{0,0,1},{0,-1,0},{0,0,-1},{-1,0,0},{1,0,0}};
node Q[100000];
node St,En;
bool flag;
int head,tail;
int f,x,y;
void bfs()
{
    vis[St.f][St.x][St.y]=true;
    St.times=0;
    Q[head++]=St;
    while(head!=tail)
    {
        node pre=Q[tail++];
        for(int i=0;i<6;i++)
        {
            node temp;
            if(pre.f==En.f&&pre.x==En.x&&pre.y==En.y){flag=true;En.times=pre.times;return ;}
            temp.f=pre.f+dir[i][0];
            temp.x=pre.x+dir[i][1];
            temp.y=pre.y+dir[i][2];
            temp.times=pre.times;
            if(temp.f>=0&&temp.f<f&&temp.x>=0&&temp.x<x&&temp.y>=0&&temp.y<y)
            {
                if(!vis[temp.f][temp.x][temp.y]&&Map[temp.f][temp.x][temp.y]!='#')
                {
                    vis[temp.f][temp.x][temp.y]=true;
                    temp.times++;
                    Q[head++]=temp;
                }
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d%d",&f,&x,&y)&&(f+y+x))
    {
        for(int i=0;i<f;i++)
          for(int j=0;j<x;j++)
          {
              scanf("%s",Map[i][j]);
               for(int k=0;k<y;k++)
                 {
                     if(Map[i][j][k]=='S')
                        {St.f=i;St.x=j;St.y=k;}
                     if(Map[i][j][k]=='E')
                        {En.f=i;En.x=j;En.y=k;}
                 }
          }
        head=tail=0;
        memset(vis,false,sizeof(vis));
        flag=false;
        bfs();
        if(flag)
        cout<<"Escaped in "<<En.times<<" minute(s)."<<endl;
        else cout<<"Trapped!"<<endl;


    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值