1. 程式人生 > >POJ2251 Dungeon Master

POJ2251 Dungeon Master

char 成了 queue struct sam col evel spa rmi

B - Dungeon Master

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides. Is an escape possible? If yes, how long will it take?

Input

L is the number of levels making up the dungeon. The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon. R and C are the number of rows and columns making up the plan of each level. Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a ‘#‘ and empty cells are represented by a ‘.‘. Your starting position is indicated by ‘S‘ and the exit by the letter ‘E‘. There‘s a single blank line after each level. Input is terminated by three zeroes for L, R and C.

Output

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form

Escaped in x minute(s).


where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line

Trapped!

Sample Input

3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3 S## #E# ### 0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

其實本質是個簡單的bfs, 幾乎不需要什麽思路,就是由二維數組變成了三維數組,增加了層的概念,即可以在總共L層內上下移動,每一層又可以左右移動,就醬

主要這裏是想試試自己的手寫循環隊列,可能實際效果在這裏不是很好吧。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <string>
  4 #include <vector>
  5 #pragma warning ( disable : 4996 )
  6 using namespace std;
  7 
  8 struct Node {
  9     int x, y, z;
 10     int step;
 11     Node() { }
 12     Node( int X, int Y, int Z )
 13         : x( X ), y( Y ), z( Z ) { step = 0; }
 14     Node& operator=( const Node &s )
 15     { this->x = s.x; this->y = s.y; this->z = s.z; this->step = s.step; return *this; }
 16 
 17 };
 18 
 19 struct queue {
 20     vector<Node> elements;
 21     int front, rear;
 22     int maxSize;
 23 
 24     queue( int n )
 25         : maxSize( n ), front( 0 ), rear( 0 ) { elements.resize(n); }
 26     ~queue() { while( front != rear ) deque(); }
 27 
 28     void enque(  Node x ) 
 29     {
 30         if ( !isfull() )
 31           {
 32             elements[ rear ] = x;
 33             rear = ( rear + 1 ) % maxSize;
 34           }
 35     }
 36 
 37     Node deque()
 38     {
 39         Node temp;
 40         if ( !isempty() )
 41           { 
 42             temp = elements[ front ];
 43             front = ( front + 1 ) % maxSize;
 44             return temp;
 45           } 
 46         return temp;
 47     }
 48 
 49     bool isfull() { return ( rear + 1 ) % maxSize == front; }
 50     bool isempty() { return rear == front; }
 51 };
 52 
 53 
 54 
 55 int L, R, C;
 56 char map[32][32][32];
 57 int     check[32][32][32];
 58 Node Start, End;
 59 
 60 bool isblock(int x, int y, int z)
 61 {
 62     if (x < 0 || x >= L || y < 0 || y >= R || z < 0 || z >= C )
 63         return true;
 64     else if ( map[x][y][z] == # )
 65         return true;
 66     else if ( check[x][y][z] == 1 )
 67         return true;
 68     else 
 69         return false;
 70 }
 71 
 72 int bfs()
 73 {
 74     queue Q(1000);
 75     Node run, next;
 76     check[Start.x][Start.y][Start.z] = 1;
 77     Q.enque(Start);
 78 
 79     while ( !Q.isempty() ) 
 80     {
 81         run = Q.deque();
 82         if( run.x == End.x && run.y == End.y && run.z == End.z )
 83             return run.step;
 84         for ( int i = 0; i < 6; i++ )
 85           {
 86             next = run;
 87             switch (i)
 88               {
 89                 case 0: next.x += 1;  break;
 90                 case 1: next.x -= 1;  break;
 91                 case 2: next.y += 1;  break;
 92                 case 3: next.y -= 1;  break;
 93                 case 4: next.z += 1;  break;
 94                 case 5: next.z -= 1;  break;
 95                 default:
 96                     break;
 97               }
 98             if( isblock(next.x, next.y, next.z) )
 99                 continue;
100             check[next.x][next.y][next.z] = 1;
101             next.step = run.step + 1;
102             Q.enque(next);
103           }
104     }
105     return 0;
106 }
107 
108 int main()
109 {
110     while ( ~scanf( "%d%d%d", &L, &R, &C ) )
111     {
112         
113         if ( L == 0 && R == 0 && C == 0 )
114             break;
115         memset( map, \0, sizeof(map) );
116         memset( check, 0, sizeof(check) );
117         for (int i = 0; i < L; i++)
118           {
119             for ( int j = 0; j < R; j++ )
120               {
121                 scanf( "%s", map[i][j] );
122                 for ( int k = 0; k < C; k++ )
123                   {
124                     if (map[i][j][k] == S)
125                         { Node s(i , j , k); Start = s; }
126                     if (map[i][j][k] == E)
127                         { Node e(i , j , k); End = e; }
128                   }
129               }
130           }
131         int fin = bfs();
132         if( fin == 0 )
133             cout << "Trapped!" << endl;
134         else 
135             cout << "Escaped in " << fin << " minute(s)." << endl;
136     }
137     return 0;
138 }

POJ2251 Dungeon Master