POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间。不同L层的地图,相同RC坐标处是相连通的。(.可走,#为墙)
解题思路:从起点开始分别往6个方向进行BFS(即入队),并记录步数,直至队为空.若一直找不到,则困住。
/* POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路) */
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; int n, m, o; //n行m列,o为层
char mapp[][][];
bool visit[][][]; //标记访问状态 /* 构造队列元素 (点位置+步数)*/
struct Point{
int x, y, z;
int step;
Point(){}
Point(int a, int b, int c, int d) :x(a), y(b), z(c), step(d){}
}; /* 判断点是否满足访问条件 */
inline bool judge(Point tmp){
if (mapp[tmp.x][tmp.y][tmp.z] == '#' || visit[tmp.x][tmp.y][tmp.z]
|| tmp.x < || tmp.x >= o || tmp.y < || tmp.y >= n
|| tmp.z < || tmp.z >= m
){
return ;
}
return ;
} /* x0为深度 y0,z0为同一层坐标 */
void bfs(int x0, int y0,int z0){
queue<Point> q;
bool found = ;
visit[x0][y0][z0] = ; //起点标记已访问
q.push(Point(x0, y0, z0, ));
while (!q.empty()){
Point tmp = q.front(); q.pop();
if (mapp[tmp.x][tmp.y][tmp.z] == 'E'){
//找到终点,搜索结束,输出最短路
printf("Escaped in %d minute(s).\n", tmp.step);
found = ;
break;
}
else{
Point tmp2;
++tmp.step; //步数必定增加 //往上一层
tmp2 = tmp;
tmp2.x = tmp.x - ;
if (judge(tmp2)){
visit[tmp2.x][tmp2.y][tmp2.z] = ;
q.push(tmp2);
} //往下一层
tmp2 = tmp;
tmp2.x = tmp.x + ;
if (judge(tmp2)){
visit[tmp2.x][tmp2.y][tmp2.z] = ;
q.push(tmp2);
} //以下四种为同一层 //往上一行
tmp2 = tmp;
tmp2.y = tmp.y - ;
if (judge(tmp2)){
visit[tmp2.x][tmp2.y][tmp2.z] = ;
q.push(tmp2);
} //往下一行
tmp2 = tmp;
tmp2.y = tmp.y + ;
if (judge(tmp2)){
visit[tmp2.x][tmp2.y][tmp2.z] = ;
q.push(tmp2);
} //往左一列
tmp2 = tmp;
tmp2.z = tmp.z - ;
if (judge(tmp2)){
visit[tmp2.x][tmp2.y][tmp2.z] = ;
q.push(tmp2);
} //往右一列
tmp2 = tmp;
tmp2.z = tmp.z + ;
if (judge(tmp2)){
visit[tmp2.x][tmp2.y][tmp2.z] = ;
q.push(tmp2);
} }//else
}//while
if (!found){
printf("Trapped!\n");
}
} int main()
{
#ifdef _LOCAL
freopen("D:\\input.txt", "r", stdin);
#endif int sx, sy, sz;
while (scanf("%d%d%d", &o, &n, &m) == && (m + n + o)){
memset(visit, , sizeof visit);
for (int i = ; i < o; ++i){
for (int j = ; j < n; ++j){
scanf("%s", mapp[i][j]);
for (int k = ; k < m; ++k){
if (mapp[i][j][k] == 'S'){
sx = i;
sy = j;
sz = k;
}
}//for(k)
}//for(j) n行
}//for(i) o层
bfs(sx, sy, sz); //sx是深度
} return ;
}
POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)的更多相关文章
- POJ 2251 Dungeon Master (三维BFS)
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
- BFS POJ 2251 Dungeon Master
题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...
- POJ 2251 Dungeon Master(地牢大师)
p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...
- 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 ...
- poj 2251 Dungeon Master
http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- POJ 2251 Dungeon Master (非三维bfs)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 55224 Accepted: 20493 ...
- POJ 2251 Dungeon Master【三维BFS模板】
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45743 Accepted: 17256 Desc ...
随机推荐
- AOP面向切面编程
1.AOP Aop(aspect object programming)面向切面编程 功能: 让关注点代码与业务代码分离! 关注点 重复代码就叫做关注点: 切面 关注点形成的类,就叫切面(类)! 面向 ...
- JS编码,解码. asp.net(C#)对应解码,编码
escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@ ...
- java---数据格式的验证
package cc.cococ.trade.util; import java.util.regex.Matcher; import java.util.regex.Pattern; public ...
- jpcap
1.System.out.println( System.getProperty("java.library.path")); 2.将jpcap.dll放到上边打印的路径中
- java基础之 switch
switch 语句的格式: switch ( 整型或字符型变量 ) { case 变量可能值1 : 分支一; break; case 变量可能值2 : 分支二; break; case 变量可 ...
- c#读写文本文档-1-用file类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- iOS 7 教程:定制iOS 7中的导航栏和状态栏
目录(?)[-] iOS 7中默认的导航栏 设置导航栏的背景颜色 在导航栏中使用背景图片 定制返回按钮的颜 修改导航栏标题的字体 修改导航栏标题为图片 添加多个按钮 修改状态栏的风格 隐藏状态栏 总结 ...
- python 中的json解析库
当一个json 数据很大的时候.load起来是很耗时的.python中常见的json解析库有cjson,simplesjson,json, 初步比较了一下, 对于loads来讲 simplejson ...
- 监听Android CTS测试项解决方案(一)
前言: 首先这里需要详细叙述一下标题中"监听Android CTS测试项解决方案"的需求.这里的需求是指我们需要精确的监听到当前CTS测试正在测试的测试项. 因为我们知道CTS认证 ...
- js 微信分享
一. //js接口 var shareme; var urls = window.location.href; if(isWeiXin()){ var weifileref=document.cr ...