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

分析:开三维数组即可。

#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. 用 lastIndexOf()、substr()、split()方法截取一段字符串

    lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索. split() 方法用于把一个字符串分割成字符串数组,抽取到分割符前面部分. subst ...

  2. Linux查看某个文件 单个字符的 个数

    查看 *****.gz 文件下的 'Cm  Dn' 单词有多少个 cat *******.gz |grep 'Cm Dn' |wc -l

  3. 「NOI2015」荷马史诗

    传送门 Luogu 解题思路 \(k\) 叉 \(\text{Huffman}\) 树板子题,至于最长串最短,只要同样权值的优先考虑深度小的就好了. 细节注意事项 咕咕咕 参考代码 #include ...

  4. python绑定调用和非绑定调用

    绑定调用和非绑定调用 在python中,绑定调用和非绑定调用其实是相对于类和实例来说的.抽象点说就是:在类实例化过程中,类的方法会绑定在实例之中,此时,这个实例会拥有这个类的具体属性和方法,这些属性和 ...

  5. 在CentOS 7环境下安装 Spark

    1.下载Spark安装包:http://mirror.bit.edu.cn/apache/spark/ 2.解压Spark的安装包并更改名称: (1)tar -zxvf spark-2.4.3-bin ...

  6. centos7下yourcompleteme安装

    以前装过一回,没成功,现在再来一次 yourcompleteme git https://github.com/ycm-core/YouCompleteMe#installation 检查软件版本 v ...

  7. 编程题目:求幂 (python)

    数值的整数次方 效率0(lgn) 这个求幂函数无论 基数 或 次方 为 正数或者为负数都是成立的.只是他们都为整数罢了. 注意了哦,这个代码必须要用python3才能运行正确,因为python3的 整 ...

  8. UVA10820 交表 Send a Table

    \(\Large\textbf{Description:} \large{输入n,求有多少个二元组(x,y)满足:1\leqslant x,y\leqslant n,且x和y互素.}\) \(\Lar ...

  9. mysql concat与concat_ws区别

    select concat('大','小') as size from 表 查询出结果为:大小 select concat('大',NULL) as size from 表 查询出结果为:null c ...

  10. Spring 配置Beans

    <bean id="UserDao" class="com.jikexueyuan.dao.impl.UserDaoImpl"> <prope ...