问题链接:UVA532 Dungeon Master。

题意简述:三维空间地牢(迷宫),每个点由'.'(可以经过)、'#'(墙)、'S'(起点)和'E'(终点)组成。移动方向有上、下、左、右、前和后6个方向。每移动一次耗费1分钟,求从'S'到'E'最快走出时间。不同L层,相同RC处是连通的。

问题分析:一个三维迷宫,典型的BFS问题。在BFS搜索过程中,走过的点就不必再走了,因为这次再走下去不可能比上次的步数少。

程序中,使用了一个队列来存放中间节点,但是每次用完需要清空。需要注意的一点,为了编程方便,终点'E'置为'.'。

需要说明的一点,用C++语言的输入比起C语言的输入,程序简洁方便。

AC的C++语言程序如下:

/* UVA532 Dungeon Master */#include <iostream>
#include <queue>using namespace std;const int DIRECTSIZE = 6;
struct direct {int dx;int dy;int dz;
} direct[DIRECTSIZE] ={{-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}};const int MAXN = 50;
char cube[MAXN][MAXN][MAXN];struct node {int x, y, z, level;
};int L, R, C;
node start, e2;
int ans;void bfs()
{queue<node> q;ans = -1;q.push(start);while(!q.empty()) {node front = q.front();q.pop();if(front.x == e2.x && front.y == e2.y && front.z == e2.z) {ans = front.level;break;}for(int i=0; i<DIRECTSIZE; i++) {int nextx, nexty, nextz;nextx = front.x + direct[i].dx;if(0 > nextx || nextx >= L)continue;nexty = front.y + direct[i].dy;if(0 > nexty || nexty >= R)continue;nextz = front.z + direct[i].dz;if(0 > nextz || nextz >= C)continue;if(cube[nextx][nexty][nextz] == '.') {cube[nextx][nexty][nextz] = '#';node v;v.x = nextx;v.y = nexty;v.z = nextz;v.level = front.level + 1;q.push(v);}}}
}int main()
{int i, j, k;char c;while(cin >> L >> R >> C) {if(L == 0 && R == 0 && C == 0)break;for(i=0; i<L; i++) {for(j=0; j<R; j++) {for(k=0; k<C; k++) {cin >> c;cube[i][j][k] = c;if(c == 'S') {start.x = i;start.y = j;start.z = k;start.level = 0;} else if(c == 'E') {e2.x = i;e2.y = j;e2.z = k;cube[i][j][k] = '.';}}}}bfs();if(ans == -1)cout << "Trapped!\n";elsecout << "Escaped in "<< ans << " minute(s)." << endl;}return 0;
}

转载于:https://www.cnblogs.com/tigerisland/p/7564452.html

UVA532 Dungeon Master相关推荐

  1. UVA532 - Dungeon Master(裸BFS)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  2. B - Dungeon Master POJ - 2251

    B - Dungeon Master POJ - 2251 题目: 逃离3D迷宫,n <=30 首先是dfs,算一下最坏情况共30层,每层有30*30的循环, 复杂度 3030*30 显然严重爆 ...

  3. POJ 2251 Dungeon Master(三维BFS求最短路径)

    3D dungeon 时间限制: 1 Sec  内存限制: 128 MB 提交: 2  解决: 2 [提交][状态][讨论版][命题人:201506020829][Edit] [TestData] 题 ...

  4. 信息学奥赛一本通(1248:Dungeon Master)

    1248:Dungeon Master 时间限制: 1000 ms         内存限制: 65536 KB 提交数: 8637     通过数: 3432 [题目描述] 这题是一个三维的迷宫题目 ...

  5. Dungeon Master 地下城大师(BFS进阶)

    题目链接:2251 -- Dungeon Master 知道你看不懂题(手动滑稽):友情链接. 题意:找到从S到E的最少步数的路径,输出该步数,不过有意思的是这个类似迷宫问题不是二维的,是一个三维迷宫 ...

  6. Dungeon Master(地牢大师、底下城主)三维bfs

    1253:Dungeon Master(地牢大师.底下城主) 要敢于变通 和普通bfs的区别,仅仅在于数组开三维+搜索方向多了上下(现在的搜索方向是 上下东南西北) bfs多组输入要记得清空队列 // ...

  7. Dungeon Master题解bfs

    Dungeon Master 题面翻译 题目描述 输入格式 输出格式 样例 #1 样例输入 #1 样例输出 #1 题解思路及TIME EXCEEDED问题 题解 超时题解(个人觉得没问题) 题面翻译 ...

  8. Dungeon Master(poj2251,bfs)

    http://poj.org/problem?id=2251 http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=15203 Dun ...

  9. BFS - Dungeon Master

    Dungeon Master 题目链接: https://vjudge.net/problem/POJ-2251 题目: You are trapped in a 3D dungeon and nee ...

最新文章

  1. ajax 和xmlHttpRequest区别
  2. VB:如何隐藏/显示treeview的ToolTips
  3. Java黑皮书课后题第5章:**5.28(显示每月第一天是周几)编写程序,提示用户输入年份和代表概念第一天是周几的数字,然后在控制台显示该年各个月份的第一天是周几
  4. jzoj3913-艰难的选择【差分,统计】
  5. anychart说明文档
  6. Android Studio 导入新工程项目
  7. 深入框架本源系列 —— Virtual Dom
  8. Kubernetes 是如何调度的?
  9. ETL异构数据源Datax_datax-web简述_09
  10. C++11 并发指南------std::thread 详解
  11. pandas—pd.DataFrame.sample
  12. FRR BGP协议分析10 -- 路由衰减
  13. pytest-fixture
  14. 2018-03-05 百度AI API说明
  15. 空空老师向青海地震捐款~~把我感动毁了
  16. BAT超强IOS面试题116道,助你拿到高薪offer
  17. 来看看小夏の算法入门——前缀和差分
  18. 【数据结构功法】第2话 · 一篇文章带你彻底吃透·算法复杂度
  19. 分享几种论文写作神器,提高你的写作效率
  20. LNMP部署+上线wordpre

热门文章

  1. 三招教你图片文字转语音怎么转
  2. zz将看板应用于软件开发:从敏捷到精益
  3. xp计算机时间与网络时间不一致,XP系统时间同步失败(无法启动Windows时间服务)的详细说明...
  4. opencv识别数码管数字
  5. 【NOIp2019模拟】题解
  6. 【安卓学习之MP3】 MP3读取格式
  7. Mac虚拟机安装win7教程
  8. Python入门习题大全——动物
  9. STM32光敏传感器实验
  10. 安捷伦网络分析仪面板介绍