POJ - 2251---Dungeon Master

本文介绍了一个基于三维迷宫的最短路径寻找问题,使用广度优先搜索(BFS)算法解决从起点到终点的最快逃脱路径。迷宫由立方体单元构成,每个单元可能为空或被岩石填充。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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!

题目大意:
输入三个数,L,R,C,代表有立体迷宫有L层,R行,C列.
.代表可以走,#代表不能走,S代表开始点,E代表结束点,
问从S开始出发,可以向6个方向走,一分钟可以走一个点,
从S走到E点,最少可以经过多少分钟,若不能到达,则输出Trapped!

这只是基本迷宫的变体而已,同样的思路,只不过多加了两个方向,变成立体的了,BFS即可,走过的一定要加访问标记。

#include<iostream>
    #include<cstring>
    #include<queue>
    using namespace std;

    struct dot{
        int x,y,z;//坐标点 
        int step;//步数即分钟数 
    };

    int l,r,c;
    char A[30][30][30];
    bool vis[30][30][30];//地图上点的访问标记 
    const int to[6][3]={{1,0,0},{-1,0,0}, //上下层 
                        {0,-1,0},{0,1,0}, //上下行 
                        {0,0,1},{0,0,-1}};//左右 

    //检查此点是否能走(能1否0) 
    int check(dot tem){
        if(tem.x<0 || tem.y<0 || tem.z<0 || tem.x>=l || tem.y>=r || tem.z>=c){//越界 
            return 0;  
        }else if(A[tem.x][tem.y][tem.z] == '#'){//墙 
            return 0;  
        }else if(vis[tem.x][tem.y][tem.z]){//已走过 
            return 0;  
        }
        return 1;  
    } 


    int BFS(dot start){
        queue<dot> Q;
        memset(vis,false,sizeof(vis));

        Q.push(start);
        vis[start.x][start.y][start.z]=true;//已访问

        while(!Q.empty()){
            dot tem = Q.front(),tem2;
            Q.pop();

            //已经到达终点  
            if(A[tem.x][tem.y][tem.z] == 'E'){
                return tem.step; 
            }

            for(int i=0 ; i<6 ; i++){
                tem2.x=tem.x+to[i][0];
                tem2.y=tem.y+to[i][1];
                tem2.z=tem.z+to[i][2];

                if(check(tem2)){//可以走 
                    vis[tem2.x][tem2.y][tem2.z]=true;//加标志 
                    tem2.step = tem.step+1;//一定要入队前加步数---------------被坑了 
                    Q.push(tem2); //入队  
                }
            } 
        }
        return 0; 
    }

    int main(void){
        dot start;
        while(cin>>l>>r>>c,l){
            for(int i=0 ; i<l ; i++){
                for(int j=0 ; j<r ; j++){
                    cin>>A[i][j];   
                    for(int k=0 ; k<c ; k++){
                        if(A[i][j][k] == 'S'){
                            start.x=i;  //起始信息赋值 
                            start.y=j;
                            start.z=k;
                            start.step=0; 
                        }
                    }
                }
            }

            int ans = BFS(start);
            if(ans != 0){
                cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
            }else{
                cout<<"Trapped!"<<endl;
            }
        }

        return 0;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值