poj 2251 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?

输入

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.

输出

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!

样例输入

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

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

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

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

0 0 0

样例输出

Escaped in 11 minute(s).
Trapped!


三维的bfs。。

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <queue>
using namespace std;

char ma[31][31][31];int vis[31][31][31];
int l,r,c,x0,y0,z0;

int dx[]={1,0,0,-1,0,0};
int dy[]={0,1,-1,0,0,0};
int dz[]={0,0,0,0,1,-1};
int time[31][31][31];

struct node
{
    int x,y,z;
    node(int a,int b,int c)
    {
        x=a;y=b;z=c;
    }
};

int bfs()
{
    queue<node> que;
    node no0(x0,y0,z0);
    vis[x0][y0][z0]=1;
    time[x0][y0][z0]=0;
    que.push(no0);
    while(!que.empty())
    {
        node no1=que.front();
        que.pop();
        for(int i=0;i<6;i++)
        {
            int x=no1.x+dx[i];
            int y=no1.y+dy[i];
            int z=no1.z+dz[i];
            if(!vis[x][y][z]&&x<l&&x>=0&&y>=0&&y<r&&z>=0&&z<c&&ma[x][y][z]!='#')
            {
                if(ma[x][y][z]=='E')
                    return time[no1.x][no1.y][no1.z]+1;
                else
                {
                    vis[x][y][z]=1;
                    que.push(node(x,y,z));
                    time[x][y][z]=time[no1.x][no1.y][no1.z]+1;
                }

            }

        }
    }
    return 0;

}

int main()
{
    while(~scanf("%d%d%d", &l,&r,&c))
    {
        if(!l&&!r&&!c)break;
        memset(vis, 0, sizeof(vis));
        memset(time, 0, sizeof(time));
        for(int i=0;i<l;i++)
            for(int j=0;j<r;j++)
            {
                scanf("%s", ma[i][j]);;
                for(int ii=0;ii<c;ii++)
                {
                     if(ma[i][j][ii]=='S')
                    {
                        x0=i;y0=j;z0=ii;
                    }
                }

            }
        int k=bfs();
        if(!k)
            printf("Trapped!\n");
        else
            printf("Escaped in %d minute(s).\n", k);
    }
}


你可能感兴趣的:(poj 2251 Dungeon Master)