POJ 2251 Dungeon Master

  毫无意义的三维BFS.....

  从 S 出发 每次可以选择六个方向 求到 E 的最小移动次数 。

  

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstdlib>
  4 #include <cstring>
  5 #include <cmath>
  6 #include <queue>
  7 
  8 using namespace std;
  9 
 10 char maze[31][31][31];
 11 bool MarkMaze[31][31][31];
 12 
 13 struct P
 14 {
 15     int l,r,c,step;
 16 }sp,ep,tp,np;
 17 
 18 int L,R,C,len;
 19 
 20 int jl[] = { 0, 0, 0, 0, 1,-1};
 21 int jr[] = {-1, 0, 1, 0, 0, 0};
 22 int jc[] = { 0,-1, 0, 1, 0, 0};
 23 
 24 void bfs()
 25 {
 26     queue<P> q;
 27     sp.step = 0;
 28     q.push(sp);
 29 
 30     while(q.empty() == false)
 31     {
 32         tp = q.front();
 33         q.pop();
 34 
 35         if(tp.l == ep.l && tp.c == ep.c && tp.r == ep.r)
 36         {
 37             len = tp.step;
 38             return ;
 39         }
 40 
 41         for(int i = 0;i < 6; ++i)
 42         {
 43             np.c = tp.c + jc[i];
 44             np.l = tp.l + jl[i];
 45             np.r = tp.r + jr[i];
 46             np.step = tp.step + 1;
 47             if(np.c >= 1 && np.c <= C && np.l >= 1 && np.l <= L && np.r >= 1 && np.r <= R && maze[np.l][np.r][np.c] != '#' && MarkMaze[np.l][np.r][np.c] == false)
 48             {
 49                 MarkMaze[np.l][np.r][np.c] = true;
 50                 q.push(np);
 51             }
 52         }
 53     }
 54 }
 55 
 56 int main()
 57 {
 58     int i,j,k;
 59 
 60     while(scanf("%d %d %d",&L,&R,&C) && (L || R || C))
 61     {
 62         len = -1;
 63 
 64         memset(MarkMaze,false,sizeof(MarkMaze));
 65 
 66         for(i = 1;i <= L; ++i)
 67         {
 68             for(j = 1;j <= R; ++j)
 69             {
 70                 scanf("%s",maze[i][j]+1);
 71             }
 72             getchar();
 73         }
 74 
 75         for(i = 1;i <= L; ++i)
 76         {
 77             for(j = 1;j <= R; ++j)
 78             {
 79                 for(k = 1;k <= C; ++k)
 80                 {
 81                     if(maze[i][j][k] == 'S')
 82                     {
 83                         sp.l = i;
 84                         sp.r = j;
 85                         sp.c = k;
 86                     }
 87                     else if(maze[i][j][k] == 'E')
 88                     {
 89                         ep.l = i;
 90                         ep.r = j;
 91                         ep.c = k;
 92                     }
 93                 }
 94             }
 95         }
 96 
 97         bfs();
 98 
 99         if(len == -1)
100             printf("Trapped!\n");
101         else
102             printf("Escaped in %d minute(s).\n",len);
103     }
104 }

 

posted @ 2013-08-20 11:12  好小孩  阅读(143)  评论(0编辑  收藏  举报