POJ 2251 Dungeon Master(bfs)

原题代号:POJ 2251

原题链接:http://poj.org/problem?id=2251

 

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 35894   Accepted: 13698

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!

题目大意:求多层迷宫中从S点到E点的最少步数,如果到达不了E则输出"Trapped!"

解题思路:简单bfs直接搞定。

AC代码:
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <iostream>
# include <fstream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <math.h>
# include <algorithm>
using namespace std;
# define pi acos(-1.0)
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define For(i,n,a) for(int i=n; i>=a; --i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define Fo(i,n,a) for(int i=n; i>a ;--i)
typedef long long LL;
typedef unsigned long long ULL;

char a[35][35][35];
int b[35][35][35];
int ans;
int cx[]= {-1,1,0,0,0,0};
int cy[]= {0,0,-1,1,0,0};
int cz[]= {0,0,0,0,-1,1};

struct node
{
    int x,y,z;
    int cnt;
};

queue<node>Q;

void bfs()
{
    while(!Q.empty())
    {
        int x=Q.front().x;
        int y=Q.front().y;
        int z=Q.front().z;
        int cnt=Q.front().cnt;
        Q.pop();
        for(int i=0; i<6; i++)
        {
            int tx=x+cx[i];
            int ty=y+cy[i];
            int tz=z+cz[i];
            if(a[tx][ty][tz]=='.'&&b[tx][ty][tz])
            {
                Q.push(node{tx,ty,tz,cnt+1});
                b[tx][ty][tz]=0;
            }
            else if(a[tx][ty][tz]=='E')
            {
                ans=cnt+1;
                break;
            }
        }
        if(ans>-1)return;
    }
}

int main()
{
    //freopen("in.txt", "r", stdin);
    int l,r,c;
    while(cin>>l>>r>>c,l&&r&&c)
    {
        mem(b,0);
        while(!Q.empty()) Q.pop();
        for(int i=1; i<=l; i++)
        {
            for(int j=1; j<=r; j++)
            {
                cin>>a[i][j]+1;
                for(int k=1; k<=c; k++)
                {
                    if(a[i][j][k]=='S')
                    {
                        Q.push(node{i,j,k,0});
                    }
                    if(a[i][j][k]!='#')
                    {
                        b[i][j][k]=1;
                    }
                }
            }
        }
        ans=-1;
        bfs();
        if(ans>-1)printf("Escaped in %d minute(s).\n",ans);
        else printf("Trapped!\n");
    }
    return 0;
}

 



posted @ 2017-07-27 09:40  韵祈  阅读(160)  评论(0编辑  收藏  举报