POJ 2251 Dungeon Master

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <queue>
  5 #define CL(x, y) memset(x,y,sizeof(x))
  6 using namespace std;
  7 const int MAX = 33;
  8 int A, B, C, i, j, k;
  9 int used[MAX][MAX][MAX];
 10 char tower[MAX][MAX][MAX];
 11 int Move[6][3]= {{1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1}};
 12 void BFS(int a, int b, int c);
 13 bool checked(int x, int y, int z);
 14 struct node
 15 {
 16     int x, y, z;
 17     int time;
 18 };
 19 queue <node> Q;
 20 int main()
 21 {
 22     while(scanf("%d%d%d",&A,&B,&C)!=EOF)
 23     {
 24         if(!A || !B || !C) break;
 25         int aa, bb, cc;
 26         int aaa, bbb, ccc;
 27         getchar();
 28         for(i = 0; i < A; i++)
 29         {
 30             for(j = 0; j < B; j++)
 31             {
 32                 for(k = 0; k < C; k++)
 33                 {
 34                     scanf("%c",&tower[i][j][k]);
 35                     if(tower[i][j][k] == 'E')
 36                     {
 37                         aa = i;
 38                         bb = j;
 39                         cc = k;
 40                     }
 41                     if(tower[i][j][k] == 'S')
 42                     {
 43                         aaa = i;
 44                         bbb = j;
 45                         ccc = k;
 46                     }
 47                 }
 48                 getchar();
 49             }
 50             if(i<A-1)
 51                 getchar();
 52         }
 53         CL(used, 0);
 54         BFS(aaa, bbb, ccc);
 55     }
 56     return 0;
 57 }
 58 void BFS(int a, int b, int c)//(a,b,c)代表所在坐标
 59 {
 60     int xx, yy, zz;
 61     while(!Q.empty())
 62         Q.pop();
 63     node first, cur, next;
 64     first.x = a;
 65     first.y = b;//这里不应该是(0,0,0)了,
 66     first.z = c;
 67     first.time = 0;
 68     used[a][b][c] = 1;
 69     Q.push(first);
 70     while(!Q.empty())
 71     {
 72         cur = Q.front();
 73         Q.pop();
 74         if(tower[cur.x][cur.y][cur.z] == 'E')
 75         {
 76             printf("Escaped in %d minute(s).
", cur.time);
 77             return ;
 78         }
 79         for(i = 0; i < 6; i++)
 80         {
 81             xx = cur.x+Move[i][0];
 82             yy = cur.y+Move[i][1];
 83             zz = cur.z+Move[i][2];
 84             if(checked(xx, yy, zz) && !used[xx][yy][zz] && tower[xx][yy][zz]!='#')//哥哥,0代表路,请看清题目!tower[xx][yy][zz]
 85             {
 86                 next.x = xx;
 87                 next.y = yy;
 88                 next.z = zz;
 89                 next.time = cur.time+1;
 90                 used[xx][yy][zz] = 1;
 91                 Q.push(next);
 92             }
 93         }
 94     }
 95     printf("Trapped!
");
 96     return ;
 97 }
 98 bool checked(int x, int y, int z)
 99 {
100     if(x>=0 && x<A && y>=0 && y<B && z>=0 && z<C)
101         return true;
102     return false;
103 }
View Code

此处错误的是开始的坐标,由于前几次都是(0,0,0)

原文地址:https://www.cnblogs.com/ghostTao/p/4326817.html