532 - Dungeon Master

  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 Specification 

The input file 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 LR 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!
 
 

题意:一个三维的空间里面,有一座牢房,其中#表示围墙。问从S出发,只能够向上下左右前后六个方向走,能否到达E。如果可以,那么最短的路径是多少


这其实就是把图的BFS拓展到空间上。是一道裸题

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
char dungeon[60][60][60];
int visit[60][60][60],l,r,c,s;
int x,y,z,fx,fy,fz;
int dir[6][3]= {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
int in(int x, int y, int z)
{
    if (x>=0 && x<l && y>=0 && y<r && z>=0 && z<c) return 1;
    return 0;
}
struct node
{
    int x,y,z;
    int s;
};
void bfs(int x, int y, int z)
{
    int i;
    node temp,now,next;
    queue<node> q;
    temp.x=x; temp.y=y; temp.z=z; temp.s=0;
    q.push(temp);
    visit[x][y][z]=1;
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for (i=0; i<6; i++)
        {
            next.x=now.x+dir[i][0];
            next.y=now.y+dir[i][1];
            next.z=now.z+dir[i][2];
            next.s=now.s+1;
            if (in(next.x,next.y,next.z) && !visit[next.x][next.y][next.z] && dungeon[next.x][next.y][next.z]!='#')
            {
                if (dungeon[next.x][next.y][next.z]=='E')
                {
                    s=next.s;
                    return;
                }
                q.push(next);
                visit[next.x][next.y][next.z]=1;
            }
        }
    }
    s=-1;
    return ;
}
int main ()
{
    int i,j,k;
    while(cin>>l>>r>>c)
    {
        if (l==0) break;
        memset(visit,0,sizeof(visit));
        for (i=0; i<l; i++)
        {
            for (j=0; j<r; j++)
            {
                for (k=0; k<c; k++)
                {
                    cin>>dungeon[i][j][k];
                    if (dungeon[i][j][k]=='S')
                    {
                        x=i;
                        y=j;
                        z=k;
                    }
                    if (dungeon[i][j][k]=='E')
                    {
                        fx=i;
                        fy=j;
                        fz=k;
                    }
                }
            }
        }
        if (x==fx && y==fy && z==fz) s=0;
        else bfs(x,y,z);
        if (s>=0) printf("Escaped in %d minute(s).\n",s);
        else cout<<"Trapped!"<<endl;

    }
    return 0;
}


你可能感兴趣的:(uva,bfs)