用了两种方式, 一种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相关推荐

  1. poj 2251 Dungeon Master (三维bfs)

    http://poj.org/problem?id=2251 简单bfs,只不过是三维的... 唯一的坑点在输出上... Escaped in %d minute(s) 这意思是答案为1输出minut ...

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

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

  3. (POJ - 2251)Dungeon Master(bfs)

    题目链接:2251 -- Dungeon Master (poj.org) 这是一个典型的bfs迷宫问题,只不过是三维的,唯一需要注意的就是输入要用cin,不要用scanf,因为有换行,其他没什么了, ...

  4. POJ 2251 - Dungeon Master + Python实现

    原题连接:2251 -- Dungeon Master 参考资料:POJ 2251 - Dungeon Master | 眈眈探求 问题:从起点S,经上下左右前后行走,到达目的地E点,返回所经过的路径 ...

  5. Dungeon Master(poj2251,bfs)

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

  6. Dungeon Master题解bfs

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

  7. POJ 2251 Dungeon Master (BFS最短路)

    三维空间里BFS最短路 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #incl ...

  8. Dungeon Master(bfs)广度优先搜索

    描述 You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of ...

  9. Dungeon Master 三维BFS

    三维BFS DungeonMasterDungeon\ MasterDungeon Master 跟二维没啥区别,就是方向多了几个,但在写的过程中出的bugbugbug还是蛮多的,所以记录一下吧! # ...

最新文章

  1. C++标准库与STL简介
  2. 如何动态的生成某种类型的集合呢_知乎画报」的移动端动态化工程实践
  3. 脚本 api_从脚本到预测API
  4. C语言带参数的main()函数
  5. springboot + vue项目跨域请求解决方案
  6. 马斯克又一个宏伟蓝图即将启动:卫星高速上网,没有中间商赚差价
  7. 你可能不知道Flutter到底有多火
  8. 拓端tecdat|R语言非线性回归nls探索分析河流阶段性流量数据和评级曲线、流量预测可视化
  9. 在c语言中保留35位小数,C语言程序设计复习题(供学有余力学生练习)(35页)-原创力文档...
  10. 评价模型的常用方法——精确率、召回率、F1 值、ROC、AUC 各自的优缺点是什么?
  11. 【AI应用】NVIDIA Tesla T4的详情参数
  12. 【剑指Offer】43. 从 1 到 n 整数中 1 出现的次数
  13. 统计学理论—假设检验
  14. C语言 结构体指针强制转换,c中结构体指针的强制类型转换
  15. 数字逻辑·逻辑代数【常用公式、化简】
  16. jsp来实现 验证码 登录案例 有图 有码
  17. Unicdoe【真正的完整码表】对照表(一)
  18. 学习浙江大学Photoshop设计精讲精练过程中的重难点及内容收获
  19. LeetCode:714. 买卖股票的最佳时机含手续费(python)
  20. 电路基础-交流电-正弦量和相量

热门文章

  1. 文件夹删不掉?有种文件夹叫 畸形文件夹
  2. 图像分割与提取:交互式前景提取(附OpenCV代码实现)
  3. 万维网、因特网和互联网的区别
  4. 全球及中国富维生素矿物质食品行业研究及十四五规划分析报告
  5. SAP CDS 开发和Fiori App生成学习笔记
  6. 抖音企业号获客系统技术操作手册
  7. 当前最为流行的可视化大屏都是怎么做的?
  8. xpath的extract()方法
  9. 【算法】求解钱币兑换问题
  10. 1200000有多少个约数