Dungeon Master POJ2251 三维BFS

题目链接: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[35][35][35];
  9 int vis[35][35][35];
 10 int x,y,z;                          //范围
 11 
 12 int d1[7] = {1,-1,0,0,0,0};
 13 int d2[7] = {0,0,1,-1,0,0};
 14 int d3[7] = {0,0,0,0,1,-1};
 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>=0 && p.x<x && p.y>=0 && p.y<y && p.z>=0 && 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] == 0){
 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=0;ii<6;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+1;
 53                         vis[tmp.x][tmp.y][tmp.z]= 1;
 54                         q.push(tmp);
 55                     }
 56                 }
 57             }
 58         }
 59 }
 60 
 61 
 62 int main()
 63 {
 64     #ifdef debug
 65     //cin重定向
 66     ifstream cin("C:\\Users\\Administrator\\Desktop\\test.txt");
 67     #endif
 68 
 69     while((cin >> x >> y >> z)){
 70         if(x == 0){
 71             break;
 72         }
 73         for(int i = 0;i < x;i++){       //读入maze
 74             for(int j = 0;j < y;j++){
 75                 for(int k = 0;k < z;k++){
 76                     cin >> a[i][j][k];
 77                     if(a[i][j][k] == 'S'){
 78                         p1.x = i;
 79                         p1.y = j;
 80                         p1.z = k;
 81                         p1.step = 0;
 82                     }else if(a[i][j][k] == 'E'){
 83                         e.x = i;
 84                         e.y = j;
 85                         e.z = k;
 86                     }
 87                 }
 88             }
 89         }
 90 
 91         memset(vis,0,sizeof(vis));      //初始化vis
 92 
 93         bfs();
 94         if(p2.x == e.x && p2.y == e.y && p2.z == e.z){
 95             cout << "Escaped in " << p2.step << " minute(s)." << endl;
 96         }else{
 97             cout << "Trapped!" << endl;
 98         }
 99     }
100 }
原文地址:https://www.cnblogs.com/SaltyFishQF/p/10289984.html