B - Dungeon Master

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.

OutputEach 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!

题目大意:

就是给定一个三维的矩阵,给定起始点,终点,#代表不能走,“.”代表可以走,然后问能否走出,若走出,输出最短时间,若不能走出,输出“Trapped!”。

思路:

首先创建一个三维的矩阵输入,然后记录下起始点,方便BFS的开始,然后再开始BFS,在判断方向时,可以将所有的方向打进数组里,通过对数组的操作实现方向的变化,注意BFS第一个找到的点就是路程最短的,这个可以根据BFS的原理推出。

一开始的时候只是打了一个数组,并不知道如何存在BFS的过程中,各个坐标的变化,这个时候看到别人的代码,知道这个地方可以用结构体来存。还有,在打方向的数组时,注意没一个单独的数组是用"{"和"}"来存入的,不是用"("和")"。。。。。。。。

代码如下:

 

 

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int num,flag;
int visited[35][35][35];
char a[35][35][35];
int l,r,c;
struct node
{
    int x,y,z,step;
};
int f[3][6]= {{1,-1,0,0,0,0},{0,0,1,-1,0,0},{0,0,0,0,1,-1}};
bool Judge(int s1,int s2,int s3)
{
    return (s1>=1&&s1<=l&&s2>=1&&s2<=r&&s3>=1&&s3<=c);
}
void BFS(int w1,int w2,int w3)
{
    queue<node>q;
    struct node c,d;
    c.x=w1;
    c.y=w2;
    c.z=w3;
    c.step=0;
    d.step=0;
    q.push(c);
    while(!q.empty())
    {
        d=q.front();
        q.pop();
        c.step=d.step+1;
        for(int i=0; i<6; i++)
        {
            c.x=d.x+f[0][i];
            c.y=d.y+f[1][i];
            c.z=d.z+f[2][i];
            if(a[c.x][c.y][c.z]!='#'&&visited[c.x][c.y][c.z]==0&&Judge(c.x,c.y,c.z))
            {
                if(a[c.x][c.y][c.z]=='E')
                {
                    flag=1;
                    num=c.step;
                    break;
                }//这个地方最好直接在for循环里面判断,防止走多余的过程
                visited[c.x][c.y][c.z]=1;
                q.push(c);
            }
        }
    }
}
int main()
{
    while(cin>>l>>r>>c)
    {
    flag=0;
    num=0;
        memset(visited,0,sizeof(visited));
        int t1,t2,t3;
        for(int i=1; i<=l; i++)
        {
            for(int j=1; j<=r; j++)
            {
                for(int k=1; k<=c; k++)
                {
                    cin>>a[i][j][k];
                    if(a[i][j][k]=='S')
                    {
                        t1=i;
                        t2=j;
                        t3=k;
                    }
                }
            }
        }
        BFS(t1,t2,t3);
        if(flag==1)
            cout<<"Escaped in "<<num<<" minute(s)."<<endl;
        else
            cout<<"Trapped!"<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值