POJ 2251

  题目大意: 给出一三维空间的地牢,要求求出由字符'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求最短路)的更多相关文章

  1. POJ 2251 Dungeon Master (三维BFS)

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

  2. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  3. POJ.2251 Dungeon Master (三维BFS)

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

  4. BFS POJ 2251 Dungeon Master

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

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

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

  6. 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 ...

  7. poj 2251 Dungeon Master

    http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  8. POJ 2251 Dungeon Master (非三维bfs)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 55224   Accepted: 20493 ...

  9. POJ 2251 Dungeon Master【三维BFS模板】

    Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45743 Accepted: 17256 Desc ...

随机推荐

  1. DB2事务日志已满的解决方法

    DB2命令终端输入: db2 update db cfg for <dbname> using LOGPRIMARY 50 db2 update db cfg for <dbname ...

  2. RPI学习--环境搭建_刷卡+wiringPi库安装

    1,镜像地址 http://www.raspberrypi.org/downloads/ 2,Windows下刷写工具 Win32 Disk Imager 3,安装wiringPi库 (这里在连网状态 ...

  3. 添加Sql作业,新建步骤出现:从IClassFactory为CLSID为{AA40D1D6-CAEF-4A56-B9BB-D0D3DC976BA2}的COM组件创建实例失败,原因是出现以下错误:c001f011。的解决方法

    32位操作系统: 打开运行(命令提示符), 一.输入 cd c:\windows\system32 进入到c:\windows\system32路径中 二.输入 regsvr32 "C:\P ...

  4. <转>用thinkPHP实现验证码的功能

    许多系统的登录都有验证码,而如果使用thinkPHP框架搭建网站的话,验证码的生成和验证就比较容易了 1.生成验证码 thinkPHP有对应生成验证码的方法 要使用验证码,需要导入扩展类库中的ORG. ...

  5. 转 如何用mt7620方案的rt2860v2驱动实现wifi探针功能,网上能搜到一些方法,但是讲的好模糊?

    原文:http://www.zhihu.com/question/33559283 如何用mt7620方案的rt2860v2驱动实现wifi探针功能,网上能搜到一些方法,但是讲的好模糊? 如何用mt7 ...

  6. “java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Timestamp”

    最近在项目中使用hibernate查询时,总报错“java.sql.SQLException: Value '0000-00-00' can not be represented as java.sq ...

  7. 使用 VisualVM 进行性能分析及调优

    VisualVM 是一款免费的性能分析工具.它通过 jvmstat.JMX.SA(Serviceability Agent)以及 Attach API 等多种方式从程序运行时获得实时数据,从而进行动态 ...

  8. tip use view.isineditmode() in your custom views to skip code when shown in eclipse

    tip use view.isineditmode() in your custom views to skip code when shown in eclipse

  9. Combination Sum 和Combination Sum II

    这两道题的基本思路和combination那一题是一致的,也是分治的方法. 其中combination Sum复杂一点,因为每个数可能用多次.仔细分析下,本质上也是一样的.原来是每个数仅两种可能.现在 ...

  10. Discuz 插件制作之后台常用函数详解

    目录 showsetting()表单显示 cpmsg()提示消息 showformheader()创建表单头 showformfooter()创建表单尾 showtableheader()创建表格头 ...