POJ.2251 Dungeon Master (三维BFS)

题意分析

你被困在一个3D地牢中且继续寻找最短路径逃生。地牢由立方体单位构成,立方体中不定会充满岩石。向上下前后左右移动一个单位需要一分钟。你不能对角线移动并且迷宫四周坚石环绕。

若能逃离,则输出逃离需要的最短时间,否则输出Trapped!。

与二维BFS的差别在于,多了一个上下两层。所以除了先后左右移动,还要有上下移动,对于每个位置,总共有6种移动方式,将6种移动方式种可行的每次塞进队列里面就好了。

依旧还要记录歩数。此题没什么大坑。

代码总览

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#define nmax 30
using namespace std;
char mp[nmax][nmax][nmax];
bool visit[nmax][nmax][nmax];
int Slevel,Sx,Sy,Elevel,Ex,Ey;
int spx[4] = {0,1,0,-1};
int spy[4] = {1,0,-1,0};
int spl[2] = {-1,1};
bool isout = false;
int l,r,c,ans;
typedef struct mes{
int level;
int x;
int y;
int steps;
}mes;
void changeVisit(mes temp)
{
visit[temp.level][temp.x][temp.y] = true;
return;
}
bool check(mes temp)
{
if(temp.level >= l || temp.level <0 || temp.x <0 || temp.x>=r || temp.y<0 || temp.y>=c || visit[temp.level][temp.x][temp.y] || mp[temp.level][temp.x][temp.y] == '#')
return false;
return true;
}
void bfs()
{
queue<mes> q;
while(!q.empty()) q.pop();
mes sta = {Slevel,Sx,Sy,0},temp,head;
q.push(sta);
changeVisit(sta);
while(!q.empty()){
head = q.front();q.pop();
if(head.level == Elevel && head.x == Ex && head.y == Ey){
isout = true;
ans = head.steps;
break;
}
for(int i = 0;i<4;++i){
temp.level = head.level;
temp.x = head.x + spx[i];
temp.y = head.y + spy[i];
temp.steps = head.steps + 1;
if(check(temp)){
q.push(temp);
changeVisit(temp);
}
}
for(int i = 0;i<2;++i){
temp.level = head.level + spl[i];
temp.x = head.x;
temp.y = head.y;
temp.steps = head.steps + 1;
if(check(temp)){
q.push(temp);
changeVisit(temp);
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d %d %d",&l,&r,&c) != EOF){
if( l == 0 && r == 0 && c == 0) break;
isout = false;
memset(mp,0,sizeof(mp));
memset(visit,0,sizeof(mp));
for(int i = 0;i<l;++i){
for(int j = 0;j<r;++j){
scanf("%s",mp[i][j]);
for(int k = 0;k<c;++k){
if(mp[i][j][k] == 'S'){
Slevel = i; Sx = j; Sy = k;
}else if(mp[i][j][k] == 'E'){
Elevel = i; Ex = j; Ey = k;
}
}
}
}
bfs();
if(isout == true){
printf("Escaped in %d minute(s).\n",ans);
}else{
printf("Trapped!\n");
}
}
return 0;
}

POJ.2251 Dungeon Master (三维BFS)的更多相关文章

  1. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  2. POJ 2251 Dungeon Master (三维BFS)

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  3. POJ - 2251 Dungeon Master 【BFS】

    题目链接 http://poj.org/problem?id=2251 题意 给出一个三维地图 给出一个起点 和 一个终点 '#' 表示 墙 走不通 '.' 表示 路 可以走通 求 从起点到终点的 最 ...

  4. (简单) POJ 2251 Dungeon Master,BFS。

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  5. poj 2251 Dungeon Master(bfs)

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  6. POJ 2251 Dungeon Master【BFS】

    题意:给出一个三维坐标的牢,给出起点st,给出终点en,问能够在多少秒内逃出. 学习的第一题三维的广搜@_@ 过程和二维的一样,只是搜索方向可以有6个方向(x,y,z的正半轴,负半轴) 另外这一题的输 ...

  7. BFS POJ 2251 Dungeon Master

    题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...

  8. POJ 2251 Dungeon Master(地牢大师)

    p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...

  9. POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索)

    POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索) Description You ar ...

随机推荐

  1. jmeter逻辑控制器

    刚开始学习,只写几种了解的逻辑控制器 1.简单控制器 只用来组合采样器和其他逻辑控制器,不影响jmeter的运行 2.循环控制器 用来循环执行采样器和其他逻辑控制器,例如一个用户发送特定请求多次,即可 ...

  2. Android测试入门学习

    一,Android测试新人练习——安装及文件传输 [课前准备] Android测试环境搭建 1.下载并安装JDK: http://www.oracle.com/technetwork/java/jav ...

  3. JMeter与WireShark

    最近在学习JMeter,刚学了一点皮毛,就掉入了WireShark的坑,我发现在学习的道路上就是不断的给自己挖坑,之前在学习LoadRunner的道路上,遇到的坑更大,就单纯的安装LR就耗费了两个星期 ...

  4. JUC——ThreadFactory线程工厂类(四)

    ThreadFactory线程工厂类 在默认情况下如果要想创建一个线程类对象,大部分情况的选择是:直接通过子类为父类进行实例化,利用Runnable子类为Runnable接口实例化. 或者直接调用La ...

  5. rz和sz上传下载文件

    安装软件包 yum install  lrzsz   上传文件,输入rz选择文件上传(可以按住shift键多选) # rz   sz 下载文件到本地,选择保存文件夹 # sz dd   xshell设 ...

  6. 3.10-通过requests、BeautifulSoup、webbrowser模块的相关方法,爬取网页数据示例程序(一)

    import requests,bs4res=requests.get('https://www.hao123.com/')print('res对象的类型:',type(res))res.raise_ ...

  7. 性能度量RMSE

    回归问题的典型性能度量是均方根误差(RMSE:Root Mean Square Error).如下公式. m为是你计算RMSE的数据集中instance的数量. x(i)是第i个实例的特征值向量 ,y ...

  8. 梯度消失&&梯度爆炸

    转载自: https://blog.csdn.net/qq_25737169/article/details/78847691 前言 本文主要深入介绍深度学习中的梯度消失和梯度爆炸的问题以及解决方案. ...

  9. ORA-28000: the account is locked 查哪个具体ip地址造成

    查系统默认的策略,连续验证10次错误帐户即会被锁 SQL> select resource_name, limit from dba_profiles where profile='DEFAUL ...

  10. ES6的新特性(2)——let 与 const 增强变量声明

    let 与 const 增强变量声明 ES6 新增了let命令,用来声明局部变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效,而且有暂时性死区的约束. 先看个var的常见 ...