POJ 2251 Dungeon Master

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23129   Accepted: 9012

Description

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

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!

Source

 
 
 
数据较小,在二维的dfs稍作修改,就能写出三维的dfs搜索了。
 
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <queue>
  4 using namespace std;
  5 
  6 struct Piont{
  7     int x;
  8     int y;
  9     int z;
 10     Piont(int x, int y, int z) :x(x), y(y), z(z){};
 11 };
 12 
 13 typedef Piont P;
 14 const int INF = 100000000;
 15 char m[35][35][35];
 16 int d[35][35][35];
 17 int dx[6] = { 1, -1, 0, 0, 0, 0 };
 18 int dy[6] = { 0, 0, 1, -1, 0, 0 };
 19 int dz[6] = { 0, 0, 0, 0, 1, -1 };
 20 
 21 int main()
 22 {
 23     int L, R, C, time, temp;
 24     int Sx, Sy, Sz, Ex, Ey, Ez;
 25 
 26     while (scanf("%d%d%d", &L, &R, &C) != EOF && L && R && C)
 27     {
 28         getchar();
 29         memset(m, 0, sizeof(m));
 30         for (int i = 0; i<L; i++)
 31         {
 32             for (int j = 0; j<R; j++)
 33             {
 34                 for (int k = 0; k<C; k++)
 35                 {
 36                     scanf("%c", &m[i][j][k]);
 37                     if (m[i][j][k] == 'S')
 38                     {
 39                         Sx = i; Sy = j; Sz = k;
 40                     }
 41                     if (m[i][j][k] == 'E')
 42                     {
 43                         Ex = i; Ey = j; Ez = k;
 44                     }
 45                 }
 46                 getchar();
 47             }
 48             getchar();
 49         }
 50 
 51         /*
 52         for (int i = 0; i<L; i++)
 53         {
 54             for (int j = 0; j<R; j++)
 55             {
 56                 for (int k = 0; k < C; k++)
 57                 {
 58                     printf("%d%c ",k, m[i][j][k]);
 59                 }
 60                 printf("\n");
 61             }
 62             
 63         }
 64         */
 65         
 66         for (int i = 0; i<L; i++)
 67             for (int j = 0; j<R; j++)
 68                 for (int k = 0; k < C; k++)
 69                     d[i][j][k] = INF;
 70 
 71         queue<P> q;
 72         q.push(P(Sx, Sy, Sz));
 73         d[Sx][Sy][Sz] = 0;
 74         
 75         while (q.size())
 76         {
 77             P p = q.front();
 78             q.pop();
 79             if (p.x == Ex && p.y == Ey && p.z == Ez)
 80                 break;
 81             for (int i = 0; i < 6; i++)
 82             {
 83                 int nx = p.x + dx[i];
 84                 int ny = p.y + dy[i];
 85                 int nz = p.z + dz[i];
 86 
 87                 if (0 <= nx&&nx < L && 0 <= ny&&ny < R && 0 <= nz&&nz < C
 88                     && m[nx][ny][nz] != '#'&&d[nx][ny][nz]==INF)
 89                 {
 90                     q.push(P(nx, ny, nz));
 91                     d[nx][ny][nz] = d[p.x][p.y][p.z] + 1;
 92                 }
 93             }
 94         }
 95 
 96         if (d[Ex][Ey][Ez]!=INF)
 97         {
 98             printf("Escaped in %d minute(s).\n", d[Ex][Ey][Ez]);
 99         }
100         else
101         {
102             printf("Trapped!\n");
103         }
104         
105     }
106 }

 

posted @ 2016-01-25 21:32  Cumulonimbus  阅读(121)  评论(0编辑  收藏  举报