题目链接:http://poj.org/problem?id=2251

题目大意

你被困在了一个三维的迷宫,找出能通往出口的最短时间。如果走不到出口,输出被困。

思路

由于要找最短路径,其实就是BFS。一般的BFS是前后左右四个方向,这个题相当于是变成能往上下左右前后六个方向找。修改一下二维BFS搜索部分的代码即可。

题解

  1. #include <iostream>
  2. #include <queue>
  3. #include<fstream>
  4. #include <cstring>
  5. //#define debug
  6. using namespace std;
  7.  
  8. char a[][][];
  9. int vis[][][];
  10. int x,y,z; //范围
  11.  
  12. int d1[] = {,-,,,,};
  13. int d2[] = {,,,-,,};
  14. int d3[] = {,,,,,-};
  15.  
  16. struct point{
  17. int x;
  18. int y;
  19. int z;
  20. int step;
  21. }p1,p2,e;
  22.  
  23. bool isValid(point p){
  24. if(p.x>= && p.x<x && p.y>= && p.y<y && p.z>= && p.z<z && (a[p.x][p.y][p.z] == '.' || a[p.x][p.y][p.z] == 'E' )&& vis[p.x][p.y][p.z] == ){
  25. return true;
  26. }
  27. return false;
  28. }
  29.  
  30. bool success(point p){
  31. if(p2.x == e.x && p2.y == e.y && p2.z == e.z){
  32. return true;
  33. }
  34. return false;
  35. }
  36.  
  37. void bfs(){
  38. point tmp;
  39. queue<point> q;
  40. q.push(p1);
  41. while(!q.empty()){
  42. p2 = q.front();
  43. q.pop();
  44. if(success(p2)){
  45. return;
  46. }else{
  47. for(int ii=;ii<;ii++){ //向六个方向搜索
  48. tmp.x = p2.x+d1[ii];
  49. tmp.y = p2.y+d2[ii];
  50. tmp.z = p2.z+d3[ii];
  51. if(isValid(tmp)){
  52. tmp.step = p2.step+;
  53. vis[tmp.x][tmp.y][tmp.z]= ;
  54. q.push(tmp);
  55. }
  56. }
  57. }
  58. }
  59. }
  60.  
  61. int main()
  62. {
  63. #ifdef debug
  64. //cin重定向
  65. ifstream cin("C:\\Users\\Administrator\\Desktop\\test.txt");
  66. #endif
  67.  
  68. while((cin >> x >> y >> z)){
  69. if(x == ){
  70. break;
  71. }
  72. for(int i = ;i < x;i++){ //读入maze
  73. for(int j = ;j < y;j++){
  74. for(int k = ;k < z;k++){
  75. cin >> a[i][j][k];
  76. if(a[i][j][k] == 'S'){
  77. p1.x = i;
  78. p1.y = j;
  79. p1.z = k;
  80. p1.step = ;
  81. }else if(a[i][j][k] == 'E'){
  82. e.x = i;
  83. e.y = j;
  84. e.z = k;
  85. }
  86. }
  87. }
  88. }
  89.  
  90. memset(vis,,sizeof(vis)); //初始化vis
  91.  
  92. bfs();
  93. if(p2.x == e.x && p2.y == e.y && p2.z == e.z){
  94. cout << "Escaped in " << p2.step << " minute(s)." << endl;
  95. }else{
  96. cout << "Trapped!" << endl;
  97. }
  98. }
  99. }

Dungeon Master POJ-2251 三维BFS的更多相关文章

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

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

  2. Dungeon Master poj 2251 dfs

    Language: Default Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16855 ...

  3. POJ 2251 三维BFS(基础题)

    Dungeon Master Description You are trapped in a 3D dungeon and need to find the quickest way out! Th ...

  4. Dungeon Master POJ - 2251 (搜索)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 48605   Accepted: 18339 ...

  5. Dungeon Master POJ - 2251 [kuangbin带你飞]专题一 简单搜索

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

  6. Dungeon Master POJ - 2251(bfs)

    对于3维的,可以用结构体来储存,详细见下列代码. 样例可以过,不过能不能ac还不知道,疑似poj炸了, #include<iostream> #include<cstdio> ...

  7. (广搜)Dungeon Master -- poj -- 2251

    链接: http://poj.org/problem?id=2251 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2137 ...

  8. kuangbin专题 专题一 简单搜索 Dungeon Master POJ - 2251

    题目链接:https://vjudge.net/problem/POJ-2251 题意:简单的三维地图 思路:直接上代码... #include <iostream> #include & ...

  9. B - Dungeon Master POJ - 2251

    //纯bfs #include <iostream> #include <algorithm> #include <cstring> #include <cs ...

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

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

随机推荐

  1. C/C++ 修改系统时间,导致sem_timedwait 一直阻塞的问题解决和分析

    修改系统时间,导致sem_timedwait 一直阻塞的问题解决和分析 介绍 最近修复项目问题时,发现当系统时间往前修改后,会导致sem_timedwait函数一直阻塞.通过搜索了发现int sem_ ...

  2. Unity Editor已停止工作

    在更换系统之后,可能会出现打开刚安装好的Unity,显示Unity Editor已停止工作,这时候我们考虑是系统win7的问题.可以在原系统上升级,也可以重新安装,升级.文中所涉及到的软件,可在右侧加 ...

  3. Python数据类型详解——列表

    Python数据类型详解--列表 在"Python之基本数据类型概览"一节中,大概介绍了列表的基本用法,本节我们详细学一下列表. 如何定义列表:在[]内以英文里输入法的逗号,,按照 ...

  4. STL中的map和multimap小结

    (1)使用map/multimap之前必须包含头文件<map>:#include<map> 并且和所有的关联式容器一样,map/multimap通常以平衡二叉树来完成    ( ...

  5. Gym 101470 题解

    A:Banks 代码: #include<bits/stdc++.h> using namespace std; #define Fopen freopen("_in.txt&q ...

  6. codeforces 743D. Chloe and pleasant prizes(树形dp)

    题目链接:http://codeforces.com/contest/743/problem/D 大致思路挺简单的就是找到一个父节点然后再找到其两个字节点总值的最大值. 可以设一个dp[x]表示x节点 ...

  7. Play on Words UVA - 10129

    题目: Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has ...

  8. yzoj P1412 & 洛谷P1629 邮递员送信 题解

    有一个邮递员要送东西,邮局在结点1.他总共要送N-1样东西,其目的地分别是2~N.由于这个城市的交通比较繁忙,因此所有的道路都是单行的,共有M条道路,通过每条道路需要一定的时间.这个邮递员每次只能带一 ...

  9. go Server示例

    示例1: package main import ( "fmt" "log" "net/http" "time" ) f ...

  10. 【Redis】安装、开启以及关闭

    一.Linux环境的操作 1.1 下载安装 1.2 启动 1.3 连接Redis客户端 1.4 关闭 二.Windows和Mac下的操作 2.1 下载安装 2.2 启动 2.3 连接客户端 2.4 关 ...