题意:三维空间求最短路,可前后左右上下移动。

分析:开三维数组即可。

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN = 30 + 10;
char pic[MAXN][MAXN][MAXN];
bool vis[MAXN][MAXN][MAXN];
int sx, sy, sz;
int dr[] = {0, 0, 1, -1, 0, 0};
int dc[] = {1, -1, 0, 0, 0, 0};
int dz[] = {0, 0, 0, 0, 1, -1};
int L, R, C;
bool judge(int x, int y, int z){
return x >= 0 && x < R && y >= 0 && y < C && z >= 0 && z < L;
}
int bfs(){
queue<int> x, y, z, step;
x.push(sx), y.push(sy), z.push(sz), step.push(0);
vis[sz][sx][sy] = true;
while(!x.empty()){
int tmpx = x.front(); x.pop();
int tmpy = y.front(); y.pop();
int tmpz = z.front(); z.pop();
int tmpstep = step.front(); step.pop();
for(int i = 0; i < 6; ++i){
int tx = tmpx + dr[i];
int ty = tmpy + dc[i];
int tz = tmpz + dz[i];
if(judge(tx, ty, tz)){
if(pic[tz][tx][ty] == 'E') return tmpstep + 1;
if(pic[tz][tx][ty] != '#' && !vis[tz][tx][ty]){
vis[tz][tx][ty] = true;
x.push(tx);
y.push(ty);
z.push(tz);
step.push(tmpstep + 1);
}
}
}
}
return -1;
}
int main(){
while(scanf("%d%d%d", &L, &R, &C) == 3){
if(!L && !R && !C) return 0;
memset(vis, false, sizeof vis);
for(int i = 0; i < L; ++i){
for(int j = 0; j < R; ++j){
scanf("%s", pic[i][j]);
for(int k = 0; k < C; ++k){
if(pic[i][j][k] == 'S'){
sz = i, sx = j, sy = k;
}
}
}
}
int ans = bfs();
if(ans == -1) printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n", ans);
}
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. POJ.2251 Dungeon Master (三维BFS)

    POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...

  8. BFS POJ 2251 Dungeon Master

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

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

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

  10. 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. 第1节 kafka消息队列:10、flume与kafka的整合使用

    11.flume与kafka的整合 实现flume监控某个目录下面的所有文件,然后将文件收集发送到kafka消息系统中 第一步:flume下载地址 http://archive.cloudera.co ...

  2. swift4之String与NSString的区别与使用

    String是结构体,NSString是类,这是它们的根本区别. 在 Swift 中,结构体struct是值类型,String是结构体,所以也是值类型.值类型被赋予给一个变量.常量或者被传递给一个函数 ...

  3. Glove - sh demo.sh出错 Error: suffix or operands invalid for `vbroadcastss'

    Glove+Python,使用预训练模型,执行sh demo.sh 报错:Error: suffix or operands invalid for `vbroadcastss' 解决:修改Makef ...

  4. 「CF1039D」You Are Given a Tree

    传送门 Luogu 解题思路 整体二分. 的确是很难看出来,但是你可以发现输出的答案都是一些可以被看作是关键字处于 \([1, n]\) 的询问,而答案的范围又很显然是 \([0, n]\),这不就刚 ...

  5. Android按返回键退出程序

    既然想实现 按两次返回键 退出程序 有两个关键词 一个是 “返回键”,再一个是“退出程序” )先说“退出” 退出相信大家都会 finish(); System.exit(); 为了确保不出现问题,两种 ...

  6. STM32学习笔记:为什么使用外部中断要打开syscfg时钟?

    AFIO时钟只是在STM32F1系列里被提及. 对于32F1系列,涉及到管脚的EXTI. REMAP.事件输出时就需要开启AFIO时钟. 比方上面提到的管脚REMAP,必须先开AFIO时钟.配置EXT ...

  7. 设备树DTS 学习: uboot 传递 dtb 给 内核

    背景 得到 dtb 文件以后,我们需要想办法下载到 板子中,并给 Linux 内核使用. (高级版本的 uboot也有了 自己使用设备树支持,我们这里不讨论 uboot 使用的设备树) Linux 内 ...

  8. Adapter之spinner

    前言: 在写代码当中有时候会用到下拉列表,下面我们讲一下spinner 正文: 因为比较简单,和之前的listView很像,所以直接上代码 <Spinner android:layout_wid ...

  9. SpringBoot邮件推送功能

    鞠躬,道歉 抱歉,迟到了近一年的更新,这一年挺忙的,发生了很多事情,就厚脸皮拖更了,抱歉. 现在状态回来了,打算分享下近期学到的东西,这一年期间学到的东西可能会随意更新,其实也就是玩了下C# + un ...

  10. 微信小程序循环中点击一个元素,其他的元素不发生变化,类似点击一个循环中的语音,其他的不发生点击事件

    类似语音,因为都在一个数据内,所以点击第一个,所有的语音都变化,解决方法就是 把整个数据都获取下来,然后更改其中一个需要更改的值,然后再把整个数据都setdata回去,如果需要动画的话,wxml里面放 ...