POJ 2251 Dungeon Master (三维BFS)

Dungeon Master
 

大意:给你一个三维的地图,可以上下左右前后的运动,求从‘S’到‘E’的对短距离。

 

思路: 训练计划上说是DFS,敲着敲着发现敲不动了,就BFS了。

 
  1 #include <stdio.h>
  2 #include <queue>
  3 #include <string.h>
  4 #include <algorithm>
  5 using namespace std;
  6 
  7 int Map[35][35][35];
  8 int s_x, s_y, s_z;
  9 int b_x, b_y, b_z;
 10 int e_x, e_y, e_z;
 11 bool dis[35][35][35];
 12 int ans[300000];
 13 int d[][3] = {{-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}};
 14 int Ans;
 15 
 16 struct node
 17 {
 18     int x, y, z;
 19 } t[300000];
 20 
 21 bool is_board(int a, int b, int c)
 22 {
 23     if(a >= 1 && a <= b_x && b >= 1 && b <= b_y && c >= 1 && c <= b_z)
 24         return true;
 25     return false;
 26 }
 27 
 28 int BFS()
 29 {
 30     int front, rear;
 31     int dx, dy, dz;
 32     memset(dis, 0, sizeof(dis));
 33     memset(ans, 0, sizeof(ans));
 34     t[0].x = s_x, t[0].y = s_y, t[0].z = s_z;
 35     //printf("%d %d %d\n", t[0].x, t[0].y, t[0].z);
 36     front = rear = 0;
 37     while(front <= rear)
 38     {
 39         for(int i = 0; i < 6; i++)
 40         {
 41             dx = t[front].x + d[i][0];
 42             dy = t[front].y + d[i][1];
 43             dz = t[front].z + d[i][2];
 44             //printf("#%d %d %d\n", dx, dy, dz);
 45             if(!dis[dx][dy][dz] && Map[dx][dy][dz] && is_board(dx, dy, dz))
 46             {
 47                 //printf("*%d %d %d\n", dx, dy, dz);
 48                 dis[dx][dy][dz] = true;
 49                 t[++rear].x = dx;
 50                 t[rear].y = dy;
 51                 t[rear].z = dz;
 52                 ans[rear] = ans[front]+1;
 53                 // printf("%d %d %d\n", e_x, e_y, e_z);
 54                 //printf("%d\n", rear);
 55                 if(dx == e_x && dy == e_y && dz == e_z)
 56                     return ans[rear];
 57             }
 58         }
 59         front++;
 60     }
 61     return 0;
 62 }
 63 
 64 void Solve()
 65 {
 66     char c;
 67     while(~scanf("%d%d%d%*c", &b_x, &b_y, &b_z))
 68     {
 69         if(!b_x && !b_y && !b_z)
 70             break;
 71         memset(Map, 0, sizeof(Map));
 72         for(int i = 1; i <= b_x; i++)
 73         {
 74             for(int j = 1; j <= b_y; j++)
 75             {
 76                 for(int k = 1; k <= b_z; k++)
 77                 {
 78                     scanf("%c", &c);
 79                     if(c == 'S')
 80                         s_x = i, s_y = j, s_z = k;
 81                     else if(c =='E')
 82                     {
 83                         e_x = i, e_y = j, e_z = k;
 84                         Map[i][j][k] = 1;
 85                     }
 86                     else if(c =='.')
 87                         Map[i][j][k] = 1;
 88                     else
 89                         Map[i][j][k] = 0;
 90                 }
 91                 getchar();
 92             }
 93             if(i != b_x)
 94                 getchar();
 95         }
 96         Ans = BFS();
 97         if(Ans)
 98             printf("Escaped in %d minute(s).\n", Ans);
 99         else
100             printf("Trapped!\n");
101     }
102 }
103 
104 int main(void)
105 {
106     Solve();
107 
108     return 0;
109 }
Dungeon Master

 

posted @ 2013-12-19 18:23  GLSilence  阅读(192)  评论(0编辑  收藏  举报