POJ 2251(Dungeon Master)

  1 /*简单BFS
  2 Author:sorry
  3 Date:2012/9/27 21:20
  4 以起始点开始,下一步有六种可能,north,south,west,east,up,down
  5 逐一列举并入队(如果合法的话),直到终点为止,若直到队列为空也
  6 不能出去的话,那就是Trapped!了
  7 剪枝:以入过队列的就不需重新入队了
  8 */
  9 #include<iostream>
 10 #include<stdio.h>
 11 #include<queue>
 12 #include<string.h>
 13 using namespace std;
 14 #define maxn 31
 15 char dungeon[maxn][maxn][maxn];
 16 bool visited[maxn][maxn][maxn];
 17 int l,r,c;
 18 int si,sj,sk;
 19 struct node
 20 {
 21     int i,j,k,t;
 22 };
 23 void BFS()
 24 {
 25     queue<node> q;
 26     node s;
 27     memset(visited,false,sizeof(visited));
 28     s.i = si,s.j = sj,s.k = sk,s.t = 0;
 29     q.push(s);
 30     visited[sk][si][sj] = true;
 31     while(!q.empty())
 32     {
 33         node temp = q.front();
 34         q.pop();
 35         node next;
 36         if(temp.i-1 >= 1 && !visited[temp.k][temp.i-1][temp.j] && dungeon[temp.k][temp.i-1][temp.j] != '#')      //north
 37         {
 38             if(dungeon[temp.k][temp.i-1][temp.j] == 'E')
 39             {
 40                 printf("Escaped in %d minute(s).\n",temp.t+1);
 41                 return;
 42             }
 43             if(dungeon[temp.k][temp.i-1][temp.j] == '.')
 44                 next.i = temp.i-1,next.j = temp.j,next.k = temp.k,next.t = temp.t+1;
 45             q.push(next),visited[next.k][next.i][next.j] = true;
 46         }
 47         if(temp.i+1 <= r && !visited[temp.k][temp.i+1][temp.j] && dungeon[temp.k][temp.i+1][temp.j] != '#')      //south
 48         {
 49             if(dungeon[temp.k][temp.i+1][temp.j] == 'E')
 50             {
 51                 printf("Escaped in %d minute(s).\n",temp.t+1);
 52                 return;
 53             }
 54             if(dungeon[temp.k][temp.i+1][temp.j] == '.')
 55                 next.i = temp.i+1,next.j = temp.j,next.k = temp.k,next.t = temp.t+1;
 56             q.push(next),visited[next.k][next.i][next.j] = true;
 57         }
 58         if(temp.j-1 >= 1 && !visited[temp.k][temp.i][temp.j-1] && dungeon[temp.k][temp.i][temp.j-1] != '#')      //west
 59         {
 60             if(dungeon[temp.k][temp.i][temp.j-1] == 'E')
 61             {
 62                 printf("Escaped in %d minute(s).\n",temp.t+1);
 63                 return;
 64             }
 65             if(dungeon[temp.k][temp.i][temp.j-1] == '.')
 66                 next.i = temp.i,next.j = temp.j-1,next.k = temp.k,next.t = temp.t+1;
 67             q.push(next),visited[next.k][next.i][next.j] = true;
 68         }
 69         if(temp.j+1 <= c && !visited[temp.k][temp.i][temp.j+1] && dungeon[temp.k][temp.i][temp.j+1] != '#')      //east
 70         {
 71             if(dungeon[temp.k][temp.i][temp.j+1] == 'E')
 72             {
 73                 printf("Escaped in %d minute(s).\n",temp.t+1);
 74                 return;
 75             }
 76             if(dungeon[temp.k][temp.i][temp.j+1] == '.')
 77                 next.i = temp.i,next.j = temp.j+1,next.k = temp.k,next.t = temp.t+1;
 78             q.push(next),visited[next.k][next.i][next.j] = true;
 79         }
 80         if(temp.k-1 >= 1 && !visited[temp.k-1][temp.i][temp.j] && dungeon[temp.k-1][temp.i][temp.j] != '#')      //up
 81         {
 82             if(dungeon[temp.k-1][temp.i][temp.j] == 'E')
 83             {
 84                 printf("Escaped in %d minute(s).\n",temp.t+1);
 85                 return;
 86             }
 87             if(dungeon[temp.k-1][temp.i][temp.j] == '.')
 88                 next.i = temp.i,next.j = temp.j,next.k = temp.k-1,next.t = temp.t+1;
 89             q.push(next),visited[next.k][next.i][next.j] = true;
 90         }
 91         if(temp.k+1 <= l && !visited[temp.k+1][temp.i][temp.j] && dungeon[temp.k+1][temp.i][temp.j] != '#')      //down
 92         {
 93             if(dungeon[temp.k+1][temp.i][temp.j] == 'E')
 94             {
 95                 printf("Escaped in %d minute(s).\n",temp.t+1);
 96                 return;
 97             }
 98             if(dungeon[temp.k+1][temp.i][temp.j] == '.')
 99                 next.i = temp.i,next.j = temp.j,next.k = temp.k+1,next.t = temp.t+1;
100             q.push(next),visited[next.k][next.i][next.j] = true;
101         }
102     }
103     printf("Trapped!\n");
104 }
105 int main()
106 {
107     //freopen("2251.txt","r",stdin);
108     while(scanf("%d%d%d",&l,&r,&c) && (l||r||c))
109     {
110         memset(dungeon,0,sizeof(dungeon));
111         int i,j,k;
112         for(k = 1; k <= l; k++)
113             for(i = 1; i <= r; i++)
114                 for(j = 1; j <= c; j++)
115                 {
116                     cin >> dungeon[k][i][j];
117                     if(dungeon[k][i][j] == 'S')
118                         sk = k,si = i,sj = j;
119                 }
120         BFS();
121     }
122     return 0;
123 }

 

posted @ 2012-09-27 21:23  sorryhao  阅读(201)  评论(0)    收藏  举报