Dungeon Master(BFS)

版权声明:转载请注明出处链接 https://blog.csdn.net/qq_43408238/article/details/89456432

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!

          题意大致是你被困在一个3D牢房里,s为起点,你需要移动一个方格需要一个单位时间,你需要求出到终点的e的最短时间。

    思路:一看这个题目,最短时间,就考虑BFS,因为是空间寻找出路,不是平面,用三维数组模拟,但是它没告诉我范围,想来也不会很大,就开个50*50*50应该就够了,接下来就是队列存储、删除节点的套路性的步骤了。思考的还不是很严密,导致花了不少时间。

Code:

#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<cmath>
#include <algorithm>
#include <stdlib.h>
#include <cstdio>
#include<sstream>
#include<cctype>
#include <queue>
#include <map>
#define ll long long
using namespace std;
char  ans[50][50][50];//存储数据
bool vis[50][50][50];//标记数组,排除重复节点
ll dirx[]={0,0,0,0,1,-1};//模拟六个方向
ll diry[]={0,0,1,-1,0,0};
ll dirz[]={1,-1,0,0,0,0};
ll n,m,k,s1,s2,s3,e1,e2,e3;
struct node
{
    ll x,y,z;
    ll step;
    node(ll a,ll b,ll c,ll d):x(a),y(b),z(c),step(d){}
};ll sum;
queue<node> v;
void init()
{
    while(!v.empty()) v.pop();
    for(ll i=1;i<=n;i++)
        for(ll j=1;j<=m;j++)
        for(ll l=1;l<=k;l++)
        vis[i][j][l]=0;
}
void dfs()
{
    while(!v.empty()) {
        node q=v.front();v.pop();

         if(q.x==e1&&q.y==e2&&q.z==e3) {sum=q.step;return ;}
         for(ll i=0;i<6;i++)
         {
              ll a=q.x+dirx[i],b=q.y+diry[i],c=q.z+dirz[i];
             if(a>=1&&a<=n&&b>=1&&b<=m&&c>=1&&c<=k&&ans[a][b][c]!='#')
             {
                 if(vis[a][b][c]==0)
                  {vis[a][b][c]=1;v.push(node(a,b,c,q.step+1));}
             }


         }

    }
}
int main()
{
    while(cin>>n>>m>>k)
    {
        if(n+m+k==0) break;
        for(ll i=1;i<=n;i++)
            for(ll j=1;j<=m;j++)
            for(ll h=1;h<=k;h++)
            {cin>>ans[i][j][h];
            if(ans[i][j][h]=='S') s1=i,s2=j,s3=h;
            if(ans[i][j][h]=='E') e1=i,e2=j,e3=h;
            }
            sum=0;
            init();
            v.push(node(s1,s2,s3,0));
            vis[s1][s2][s3]=1;
            dfs();
            if(sum) cout<<"Escaped in "<<sum<<" minute(s)."<<endl;
            else  cout<<"Trapped!"<<endl;
    }
}

 

猜你喜欢

转载自blog.csdn.net/qq_43408238/article/details/89456432