(三维BFS)Dungeon Master

传送门: Dungeon Master 

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!

解题感悟:第一次做三维bfs的题,开始运行的时候运行不出结果,原来是又忘了设置访问标记。做搜索类的题目一定要设置访问标记呀....思路很简单,直接上代码吧

#include<iostream>
#include<cstdio>
#include<string>
#include<string.h>
#include<queue>
using namespace std;
const int maxn=35;
char mp[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int l,r,c;

typedef struct Node{
    int x,y,z,step;
}Node;
int dir[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};


int bfs(int x,int y,int z)
{
    memset(vis,0,sizeof(vis));
    queue<Node> q;
    Node tmp,nxt;
    tmp.x=x;
    tmp.y=y;
    tmp.z=z;
    tmp.step=0;
    vis[z][x][y]=1;
    q.push(tmp);
    while(!q.empty()){
       // cout<<"BBB"<<endl;
        tmp=q.front();
        q.pop();
        for(int i=0;i<6;i++){
            nxt.x=tmp.x+dir[i][0];
            nxt.y=tmp.y+dir[i][1];
            nxt.z=tmp.z+dir[i][2];
            if(nxt.x>=0&&nxt.y>=0&&nxt.z>=0&&nxt.x<r&&nxt.y<c&&nxt.z<l&&!vis[nxt.z][nxt.x][nxt.y]){
                nxt.step=tmp.step+1;
                if(mp[nxt.z][nxt.x][nxt.y]=='E'){
                    //cout<<"LLLL"<<endl;
                    return nxt.step;
                }else if(mp[nxt.z][nxt.x][nxt.y]=='.')
                {
                    //cout<<"x:"<<nxt.z<<",y:"<<nxt.x<<",z:"<<nxt.y<<endl;
                    vis[nxt.z][nxt.x][nxt.y]=1;
                    q.push(nxt);
                }

            }
        }
    }
    return 0;
}

int main()
{
    while(~scanf("%d%d%d",&l,&r,&c))
    {
        if(l==0&&r==0&&c==0)
            break;
        int x,y,z;
        bool flag=false;
        for(int i=0;i<l;i++){
            for(int j=0;j<r;j++){
                scanf("%s",mp[i][j]);
                if(!flag){
                    for(int k=0;k<c;k++){
                        if(mp[i][j][k]=='S'){
                            z=i;
                            x=j;
                            y=k;
                            flag=true;
                        }
                    }
                }
            }
        }
        //cout<<"AAA"<<endl;
        int ans=bfs(x,y,z);
        if(ans)
            printf("Escaped in %d minute(s).\n",ans);
        else
            printf("Trapped!\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37275680/article/details/80314828
今日推荐