POJ 2251-Dungeon Master(BFS)[通俗易懂]

POJ 2251-Dungeon Master(BFS)

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17007   Accepted: 6620

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
3维地图给起点和终点搜最短路。

。刷dfs专题刷出来这玩意也是醉了。。

BFS暴搜
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define ll long long
#define maxn 360
#define pp pair<int,int>
#define INF 0x3f3f3f3f
#define max(x,y) ( ((x) > (y)) ? (x) : (y) )
#define min(x,y) ( ((x) > (y)) ? (y) : (x) )
using namespace std;
int n,m,sx,sy,sz,A,B,C;
bool vis[105][105][105];
char ma[105][105][105];
struct node
{
    int x,y,z,step;
};
int mv[6][3]={{0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}};
void bfs()
{
    queue <node> Q;
    node  t;
    t.x=sx;t.y=sy;t.z=sz;t.step=0;
    vis[sx][sy][sz]=1;
    Q.push(t);
    while(!Q.empty())
    {
        node f=Q.front();Q.pop();
        if(ma[f.x][f.y][f.z]=='E')
        {
			printf("Escaped in %d minute(s).\n",f.step);
			return ;
        }
        for(int i=0;i<6;i++)
        {
            t.x=f.x+mv[i][0];
            t.y=f.y+mv[i][1];
            t.z=f.z+mv[i][2];
            if(0<=t.x&&t.x<A&&0<=t.y&&t.y<B&&0<=t.z&&t.z<C&&!vis[t.x][t.y][t.z]&&ma[t.x][t.y][t.z]!='#')
            {
                vis[t.x][t.y][t.z]=1;
                t.step=f.step+1;
                Q.push(t);
            }
        }
    }
    puts("Trapped!");
}
int main()
{
    while(scanf("%d%d%d",&A,&B,&C)!=EOF)
    {
    	if(!A&&!B&&!C)break;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<A;i++)
            for(int j=0;j<B;j++)
				scanf("%s",ma[i][j]);
		for(int i=0;i<A;i++)
			for(int j=0;j<B;j++)
				for(int k=0;k<C;k++)
				if(ma[i][j][k]=='S')
			    {
					sx=i;sy=j;sz=k;
					break;
		        }
            bfs();
    }
    return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116741.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 《前端运维》一、Linux基础–02用户与权限

    其实说真的,这些基础挺枯燥的,内容呢绝大多数都是些静态的。上一篇文章我们学习了基本的指令和vim编辑器的操作方法。这篇文章我们主要来学习下Linux中用户的概念和权限相关的知识。一、用户与用户组

    2022年3月25日
    43
  • 彻底理解position与anchorPoint

    彻底理解position与anchorPoint原文  http://www.cnblogs.com/benbenzhu/p/3615516.html引言相信初接触到CALayer的人都会遇到以下几个问题:  为什么修改anchorPoint会移动layer的位置? CALayer的position点是哪一点呢? anchorPoint与position有什么关系?我也迷惑过,找过网上的教程,

    2022年10月8日
    3
  • Mac PHPStorm清除SVN配置缓存

    Mac PHPStorm清除SVN配置缓存

    2021年10月21日
    40
  • unity2d3d结合_unity3d脚本编程与游戏开发

    unity2d3d结合_unity3d脚本编程与游戏开发Unity3D数字孪生笔记(八)一、脚本介绍1、脚本1>介绍2>语法结构3>编译过程4>修改脚本模板2、开发工具1>MonoDevelop2>VisualStudio3>Console3、脚本生命周期4、调试1>使用Unity编辑器2>使用VS3>使用MonoDevelop二、常用API1、Component2、Transform3、GameObject4、Time一、脚本介绍1、脚本1>介绍脚本是附加在游戏物体上用于定义游戏对

    2022年9月18日
    3
  • mysql导入excel文件_将Excel数据导入MySQL「建议收藏」

    mysql导入excel文件_将Excel数据导入MySQL「建议收藏」去年的投资统计月报数据量庞大,原始表格是xls格式(还是EXECL2003的),单个sheet最大只能放几万行,但数据总量有10万行以上,于是只能存成两个sheet。EXECL2010格式倒是单个sheet可以放得下,可是居然不能将数据完整的从一个sheet复制粘贴到另一个sheet(可能是因为行数太多)。正好想学习一下execl数据导入MySQL数据库的方法,于是开始尝试。一开始使用的是MySQ…

    2025年11月19日
    9
  • dirsearch讲解_mv命令使用

    dirsearch讲解_mv命令使用dirsearch用法github地址参数选项(机翻)强制:字典设置:常规设置:请求设置:github地址https://github.com/maurosoria/dirsearch参数选项(机翻)强制:Mandatory:-uURL,–url=URL 目标URL-lFILE,–url-list=FILE 目标URL列表文件–stdin TargetURLlistfromSTDIN–cidr=CIDR

    2022年10月5日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号