UVA 532 Dungeon Master

题目如下:

Dungeon Master 

You are trapped in a 3D dungeon and need to find the quickest way out!The dungeon is composedof unit cubes which may or may not be filled with rock. It takes one minuteto move one unit north,south, east, west, up or down. You cannot move diagonally and the maze issurrounded by solid rock on all sides.


Is an escape possible? If yes, how long will it take?

Input Specification  The input file consists of a number of dungeons. Each dungeon descriptionstarts with a linecontaining 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 describesone 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 blankline after each level. Input is terminated by three zeroes for L, R and C.

Output Specification  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,和二维没什么区别,改成三位数组就行了,1A。

AC的代码如下:

#include 
   
    
using namespace std;
struct point
{
    int x1;
    int y1;
    int z1;
};
point q[30*30*30+5];
char graph[30][30][30];
int dist[30][30][30];
int dx[]= {0,0,0,0,1,-1};
int dy[]= {0,0,-1,1,0,0};
int dz[]= {-1,1,0,0,0,0};
int vis[30][30][30];
int L,R,C;
void bfs(int x,int y,int z)
{
    int front=0,rear=0;
    point a;
    a.x1=x;
    a.y1=y;
    a.z1=z;
    q[rear++]=a;
    vis[x][y][z]=1;
    while(front
    
     =0newy=0newz=0newx
     
      
       
        LRC) { char c; int x,y,z,ex,ey,ez; memset(vis,0,sizeof vis); memset(dist,0,sizeof dist); if(L==0) break; for(int i=0; i
        
         c; if(c!=' 'c!='\n') graph[i][j][k]=c; if(c=='S') { x=i; y=j; z=k; } if(c=='E') { ex=i; ey=j; ez=k; } } bfs(x,y,z); if(dist[ex][ey][ez]!=0) cout"Escaped in "
         
          " minute(s)."
          
         
        
       
      
     
    
   


最新回复(0)
/jishuZ7qonte_2Bm4lxUHvJsZTOM4Y_2FTVXTD7PG728tcg_3D_3D4795030
8 简首页