pku 2251 Dungeon Master 基本BFS
用了两种方式, 一种stl队列,一种自己实现的队列,事实证明stl就是好呀.
stl万岁.
#include <iostream> #include <queue> using namespace std; int map[30][30][30]; bool visited[30][30][30]; int startx, starty, startz; int endx, endy, endz; int l, h, w; int dx[] = {-1, 0, 1, 0, 0, 0}; int dy[] = {0, -1, 0, 1, 0, 0}; int dz[] = {0, 0, 0, 0, -1, 1}; inline bool ok(int x, int y, int z) { if(z < 0 || z >= l || y < 0 || y >= h || x < 0 || x >= w) return false; return true; } struct Point{ int x, y, z; Point(int xx, int yy, int zz) {x =xx;y=yy;z=zz;} }; int step[30][30][30]; int bfs() { memset(visited, 0, sizeof(visited)); memset(step, 0, sizeof(step)); queue<Point> q; q.push(Point(startx, starty, startz)); visited[startz][starty][startx] =true; while (!q.empty()) { Point p = q.front(); if(p.x == endx && p.y == endy && p.z == endz) break; q.pop(); for(int i = 0; i < 6; ++i) { int tempx = p.x+dx[i], tempy = p.y+dy[i], tempz = p.z+dz[i]; if(ok(tempx, tempy, tempz) && map[tempz][tempy][tempx] && !visited[tempz][tempy][tempx]) { step[tempz][tempy][tempx] = step[p.z][p.y][p.x]+1; q.push(Point(tempx, tempy, tempz)); visited[tempz][tempy][tempx] = true; } } } return step[endz][endy][endx]; } int main() { char ch; while(scanf("%d%d%d", &l, &h, &w) && !(l==0&&h==0&&w==0)) { for(int i = 0; i < l; ++i) for(int j = 0; j < h; ++j) for(int k = 0; k < w; ++k) { cin >> ch; if(ch == '.') map[i][j][k] = 1; else if(ch == '#') map[i][j][k] = 0; else if(ch == 'E') { startz = i, starty = j, startx = k; map[i][j][k] = 2; } else { endz = i, endy = j, endx = k; map[i][j][k] = 3; } } int ret = bfs(); if(ret) printf("Escaped in %d minute(s)./n", ret); else printf("Trapped!/n"); } return 0; }
#include <iostream> #include <queue> using namespace std; int map[30][30][30]; bool visited[30][30][30]; int startx, starty, startz; int endx, endy, endz; int l, h, w; int dx[] = {-1, 0, 1, 0, 0, 0}; int dy[] = {0, -1, 0, 1, 0, 0}; int dz[] = {0, 0, 0, 0, -1, 1}; inline bool ok(int x, int y, int z) { if(z < 0 || z >= l || y < 0 || y >= h || x < 0 || x >= w) return false; return true; } struct Point{ int x, y, z; }; int step[30][30][30]; Point q[27000]; int bfs() { memset(visited, 0, sizeof(visited)); memset(step, 0, sizeof(step)); int rear = -1, front = -1; q[++rear].x = startx; q[rear].y = starty; q[rear].z = startz; visited[startz][starty][startx] =true; while (front < rear) { Point p; p.x = q[++front].x; p.y = q[front].y; p.z = q[front].z; if(p.x == endx && p.y == endy && p.z == endz) break; for(int i = 0; i < 6; ++i) { int tempx = p.x+dx[i], tempy = p.y+dy[i], tempz = p.z+dz[i]; if(ok(tempx, tempy, tempz) && map[tempz][tempy][tempx] && !visited[tempz][tempy][tempx]) { step[tempz][tempy][tempx] = step[p.z][p.y][p.x]+1; q[++rear].x = tempx; q[rear].y = tempy; q[rear].z = tempz; visited[tempz][tempy][tempx] = true; } } } return step[endz][endy][endx]; } int main() { char ch; while(scanf("%d%d%d", &l, &h, &w) && !(l==0&&h==0&&w==0)) { for(int i = 0; i < l; ++i) for(int j = 0; j < h; ++j) for(int k = 0; k < w; ++k) { cin >> ch; if(ch == '.') map[i][j][k] = 1; else if(ch == '#') map[i][j][k] = 0; else if(ch == 'E') { startz = i, starty = j, startx = k; map[i][j][k] = 2; } else { endz = i, endy = j, endx = k; map[i][j][k] = 3; } } int ret = bfs(); if(ret) printf("Escaped in %d minute(s)./n", ret); else printf("Trapped!/n"); } return 0; }
pku 2251 Dungeon Master 基本BFS相关推荐
- poj 2251 Dungeon Master (三维bfs)
http://poj.org/problem?id=2251 简单bfs,只不过是三维的... 唯一的坑点在输出上... Escaped in %d minute(s) 这意思是答案为1输出minut ...
- POJ 2251 Dungeon Master(三维BFS求最短路径)
3D dungeon 时间限制: 1 Sec 内存限制: 128 MB 提交: 2 解决: 2 [提交][状态][讨论版][命题人:201506020829][Edit] [TestData] 题 ...
- (POJ - 2251)Dungeon Master(bfs)
题目链接:2251 -- Dungeon Master (poj.org) 这是一个典型的bfs迷宫问题,只不过是三维的,唯一需要注意的就是输入要用cin,不要用scanf,因为有换行,其他没什么了, ...
- POJ 2251 - Dungeon Master + Python实现
原题连接:2251 -- Dungeon Master 参考资料:POJ 2251 - Dungeon Master | 眈眈探求 问题:从起点S,经上下左右前后行走,到达目的地E点,返回所经过的路径 ...
- Dungeon Master(poj2251,bfs)
http://poj.org/problem?id=2251 http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=15203 Dun ...
- Dungeon Master题解bfs
Dungeon Master 题面翻译 题目描述 输入格式 输出格式 样例 #1 样例输入 #1 样例输出 #1 题解思路及TIME EXCEEDED问题 题解 超时题解(个人觉得没问题) 题面翻译 ...
- POJ 2251 Dungeon Master (BFS最短路)
三维空间里BFS最短路 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #incl ...
- Dungeon Master(bfs)广度优先搜索
描述 You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of ...
- Dungeon Master 三维BFS
三维BFS DungeonMasterDungeon\ MasterDungeon Master 跟二维没啥区别,就是方向多了几个,但在写的过程中出的bugbugbug还是蛮多的,所以记录一下吧! # ...
最新文章
- C++标准库与STL简介
- 如何动态的生成某种类型的集合呢_知乎画报」的移动端动态化工程实践
- 脚本 api_从脚本到预测API
- C语言带参数的main()函数
- springboot + vue项目跨域请求解决方案
- 马斯克又一个宏伟蓝图即将启动:卫星高速上网,没有中间商赚差价
- 你可能不知道Flutter到底有多火
- 拓端tecdat|R语言非线性回归nls探索分析河流阶段性流量数据和评级曲线、流量预测可视化
- 在c语言中保留35位小数,C语言程序设计复习题(供学有余力学生练习)(35页)-原创力文档...
- 评价模型的常用方法——精确率、召回率、F1 值、ROC、AUC 各自的优缺点是什么?
- 【AI应用】NVIDIA Tesla T4的详情参数
- 【剑指Offer】43. 从 1 到 n 整数中 1 出现的次数
- 统计学理论—假设检验
- C语言 结构体指针强制转换,c中结构体指针的强制类型转换
- 数字逻辑·逻辑代数【常用公式、化简】
- jsp来实现 验证码 登录案例 有图 有码
- Unicdoe【真正的完整码表】对照表(一)
- 学习浙江大学Photoshop设计精讲精练过程中的重难点及内容收获
- LeetCode:714. 买卖股票的最佳时机含手续费(python)
- 电路基础-交流电-正弦量和相量