UVA532 Dungeon Master
问题链接: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相关推荐
- UVA532 - Dungeon Master(裸BFS)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- B - Dungeon Master POJ - 2251
B - Dungeon Master POJ - 2251 题目: 逃离3D迷宫,n <=30 首先是dfs,算一下最坏情况共30层,每层有30*30的循环, 复杂度 3030*30 显然严重爆 ...
- POJ 2251 Dungeon Master(三维BFS求最短路径)
3D dungeon 时间限制: 1 Sec 内存限制: 128 MB 提交: 2 解决: 2 [提交][状态][讨论版][命题人:201506020829][Edit] [TestData] 题 ...
- 信息学奥赛一本通(1248:Dungeon Master)
1248:Dungeon Master 时间限制: 1000 ms 内存限制: 65536 KB 提交数: 8637 通过数: 3432 [题目描述] 这题是一个三维的迷宫题目 ...
- Dungeon Master 地下城大师(BFS进阶)
题目链接:2251 -- Dungeon Master 知道你看不懂题(手动滑稽):友情链接. 题意:找到从S到E的最少步数的路径,输出该步数,不过有意思的是这个类似迷宫问题不是二维的,是一个三维迷宫 ...
- Dungeon Master(地牢大师、底下城主)三维bfs
1253:Dungeon Master(地牢大师.底下城主) 要敢于变通 和普通bfs的区别,仅仅在于数组开三维+搜索方向多了上下(现在的搜索方向是 上下东南西北) bfs多组输入要记得清空队列 // ...
- Dungeon Master题解bfs
Dungeon Master 题面翻译 题目描述 输入格式 输出格式 样例 #1 样例输入 #1 样例输出 #1 题解思路及TIME EXCEEDED问题 题解 超时题解(个人觉得没问题) 题面翻译 ...
- Dungeon Master(poj2251,bfs)
http://poj.org/problem?id=2251 http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=15203 Dun ...
- BFS - Dungeon Master
Dungeon Master 题目链接: https://vjudge.net/problem/POJ-2251 题目: You are trapped in a 3D dungeon and nee ...
最新文章
- ajax 和xmlHttpRequest区别
- VB:如何隐藏/显示treeview的ToolTips
- Java黑皮书课后题第5章:**5.28(显示每月第一天是周几)编写程序,提示用户输入年份和代表概念第一天是周几的数字,然后在控制台显示该年各个月份的第一天是周几
- jzoj3913-艰难的选择【差分,统计】
- anychart说明文档
- Android Studio 导入新工程项目
- 深入框架本源系列 —— Virtual Dom
- Kubernetes 是如何调度的?
- ETL异构数据源Datax_datax-web简述_09
- C++11 并发指南------std::thread 详解
- pandas—pd.DataFrame.sample
- FRR BGP协议分析10 -- 路由衰减
- pytest-fixture
- 2018-03-05 百度AI API说明
- 空空老师向青海地震捐款~~把我感动毁了
- BAT超强IOS面试题116道,助你拿到高薪offer
- 来看看小夏の算法入门——前缀和差分
- 【数据结构功法】第2话 · 一篇文章带你彻底吃透·算法复杂度
- 分享几种论文写作神器,提高你的写作效率
- LNMP部署+上线wordpre