1. 程式人生 > >POJ 2251 三維BFS(基礎題)

POJ 2251 三維BFS(基礎題)

art cube find bsp ssi cal input nal sample

Dungeon Master

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

Ulm Local 1997 思路:比較二維的增加兩種轉移方式,其他基本不變。 代碼:
 1 #include "cstdio"
 2 #include "stdlib.h"
 3 #include "iostream"
 4 #include "algorithm"
 5 #include "string"
 6 #include "cstring"
 7 #include "queue"
 8 #include "cmath"
 9 #include "vector"
10 #include "map"
11 #include "set"
12 #define mj
13 #define db double
14 #define ll long long
15 using namespace std;
16 const int N=1e8+5;
17 const int mod=1e9+7;
18 const ll inf=1e16+10;
19 bool v[35][35][35];
20 int dx[6]={0,0,1,0,-1,0},dy[6]={0,0,0,1,0,-1},dz[6]={-1,1,0,0,0,0};
21 int a,b,c;
22 char s[35][35][35];
23 int d[35][35][35];
24 typedef struct P{
25     int x,y,z;
26     P(int c,int d,int e){
27         z=c,x=d,y=e;
28     }
29 };
30 void bfs(P t){
31     queue<P> q;
32     for(int i=0;i<35;i++)
33         for(int j=0;j<35;j++)
34             for(int k=0;k<35;k++)
35                 d[i][j][k]=N;
36     q.push(t);
37     memset(v,0, sizeof(v));
38     d[t.z][t.x][t.y]=0;
39     v[t.z][t.x][t.y]=1;
40     while(q.size()){
41         P p=q.front();
42         q.pop();
43         if(s[p.z][p.x][p.y]==E){
44             printf("Escaped in %d minute(s).\n",d[p.z][p.x][p.y]);
45             return;
46         }
47 
48         for(int i=0;i<6;i++){
49             int nx=p.x+dx[i],ny=p.y+dy[i],nz=p.z+dz[i];
50             if(0<=nx&&nx<b&&0<=ny&&ny<c&&0<=nz&&nz<a&&s[nz][nx][ny]!=#&&!v[nz][nx][ny]){
51                 d[nz][nx][ny]=d[p.z][p.x][p.y]+1;
52                 q.push(P(nz,nx,ny));
53                 v[nz][nx][ny]=1;
54 
55             }
56         }
57 
58     }
59     printf("Trapped!\n");
60 
61 
62 }
63 int main()
64 {
65     while(scanf("%d%d%d",&a,&b,&c)==3,a||b||c){
66         int t=a;
67         int x,y,z;
68         memset(s,\0, sizeof(s));// 一開始把x,y,z搞反了,調了好一會
69         for(int i=0;i<t;i++){
70             for(int j=0;j<b;j++){
71                 scanf("%s",s[i][j]);
72                 for(int k=0;k<c;k++)
73                     if(s[i][j][k]==S) x=j,y=k,z=i;
74             }
75         }
76         bfs(P(z,x,y));
77     }
78     return 0;
79 }

POJ 2251 三維BFS(基礎題)