POJ 2251 Dungeon Master

本文介绍了一道关于3D迷宫最短路径的编程题,使用宽度优先搜索(BFS)算法解决从起点到终点的最短步数问题。详细解释了三维数组的构建方式及遍历方向,并提供了AC代码。

题意:输入3个数,代表层数、行数、列数。即3维数组大小,输入以0 0 0为结束。接下来输入迷宫,S表示起点,E表示终点,'.'是可以走的路,'#'是不能走的路。 求从S走到E所需步数,按照题目要求输出。

简单BFS题。只不过地图变成了3维,从原先的4个方向变成了6个方向。统计步数就ok啦~ 刚入门BFS,代码是参照了一个大神的。不懂的地方可以看注释~写的还是比较详细的

Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20490 Accepted: 7937

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!


题目链接 POJ 2251

附上AC代码:

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

char maze[35][35][35];  //记录地图
int vis[35][35][35];  //标记是否已访问
int k,n,m,sx,sy,sz,ex,ey,ez;//输入的行列高,以及起始点坐标和终止点坐标
int to[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};

struct node   //坐标点,存放坐标以及步数
{
    int x,y,z,step;
};

int check(int x,int y,int z)   //写了一个check函数。判断该点是否可行。不可行返回1,可行返回0
{
    if(x<0 || y<0 || z<0 || x>=k || y>=n || z>=m) //是否越出地图边界
        return 1;
    else if(maze[x][y][z]=='#') //该点在地图上是否可行
        return 1;
    else if(vis[x][y][z])  //该点是否已访问
        return 1;
    return 0;
}

int bfs()
{
    node cur,next;       //cur表示当前点,next表示接下来要走的点
    queue<node> Q;
    cur.x=sx;           //当前点初始化。
    cur.y=sy;
    cur.z=sz;
    cur.step=0;          //步数初始化0
    vis[sx][sy][sz]=1;   //将该点设为已访问。
    Q.push(cur);         //cur入队
    while(!Q.empty())       //如果队列非空
    {
        cur=Q.front();
        Q.pop();                           //出队
        if(cur.x==ex && cur.y==ey && cur.z==ez) //如果到达终点,返回步数
            return cur.step;
        for(int i=0;i<6;i++)
        {
            next=cur;
            next.x=cur.x+to[i][0];
            next.y=cur.y+to[i][1];
            next.z=cur.z+to[i][2];
            if(check(next.x,next.y,next.z)) //如果next点不可行,继续走下个方向。
                continue;
            vis[next.x][next.y][next.z]=1;  //若可行,走该点,并标记为已访问。
            next.step=cur.step+1;           //步数+1.
            Q.push(next);                   //入队
        }
    }
    return 0;
}

int main()
{
    int i,j,l;
    while(scanf("%d%d%d",&k,&n,&m)!=EOF)
    {
        if(k==0 || n==0 || m==0)
            break;
        for(i=0;i<k;i++)
        {
            for(j=0;j<n;j++)
            {
                scanf("%s",maze[i][j]);  //输入迷宫
                for(l=0;l<m;l++)
                {
                    if(maze[i][j][l]=='S')   //定位S点坐标
                    {
                        sx=i;
                        sy=j;
                        sz=l;
                    }
                    else if(maze[i][j][l]=='E')  //定位E点坐标
                    {
                        ex=i;
                        ey=j;
                        ez=l;
                    }
                }
            }
        }
        memset(vis,0,sizeof(vis));
        int ans=bfs();
        if(ans==0)
            printf("Trapped!\n");
        else
            printf("Escaped in %d minute(s).\n",ans);
    }

    return 0;
}


源码地址: https://pan.quark.cn/s/d1f41682e390 miyoubiAuto 米游社每日米游币自动化Python脚本(务必使用Python3) 8更新:更换cookie的获取地址 注意:禁止在B站、贴吧、或各大论坛大肆传播! 作者已退游,项目不维护了。 如果有能力的可以pr修复。 小引一波 推荐关注几个非常可爱有趣的女孩! 欢迎B站搜索: @嘉然今天吃什么 @向晚大魔王 @乃琳Queen @贝拉kira 第三方库 食用方法 下载源码 在Global.py中设置米游社Cookie 运行myb.py 本地第一次运行时会自动生产一个文件储存cookie,请勿删除 当前仅支持单个账号! 获取Cookie方法 浏览器无痕模式打开 http://user.mihoyo.com/ ,登录账号 按,打开,找到并点击 按刷新页面,按下图复制 Cookie: How to get mys cookie 当触发时,可尝试按关闭,然后再次刷新页面,最后复制 Cookie。 也可以使用另一种方法: 复制代码 浏览器无痕模式打开 http://user.mihoyo.com/ ,登录账号 按,打开,找到并点击 控制台粘贴代码并运行,获得类似的输出信息 部分即为所需复制的 Cookie,点击确定复制 部署方法--腾讯云函数版(推荐! ) 下载项目源码和压缩包 进入项目文件夹打开命令行执行以下命令 xxxxxxx为通过上面方式或取得米游社cookie 一定要用双引号包裹!! 例如: png 复制返回内容(包括括号) 例如: QQ截图20210505031552.png 登录腾讯云函数官网 选择函数服务-新建-自定义创建 函数名称随意-地区随意-运行环境Python3....
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值