B_Dungeon Master(POJ_2251)

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!

练习的第一道bfs水题,感觉没有什么可讲的。。。只要思路清晰外加不犯我犯的种种弱智错误就能A掉。

代码

#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;

const int INF=0x3f3f3f3f;
const int MAXN=35;
typedef long long LL;
typedef pair<int, int> p;

int L,R,C;
char G[MAXN][MAXN][MAXN];
int d[MAXN][MAXN][MAXN];//记录到达每个点需要的时间
int dx[]={1,-1,0,0,0,0};
int dy[]={0,0,1,-1,0,0};
int dz[]={0,0,0,0,1,-1};
int sx,sy,sz,ex,ey,ez;
struct node
{
    int x,y,z;
    node(int x,int y,int z):x(x),y(y),z(z){}
};

bool ok(int x,int y,int z)//判断当前点可不可达
{
    if(d[x][y][z]==-1&&x>=0&&x<R&&y>=0&&y<C&&z>=0&&z<L&&G[x][y][z]!='#')
        return true;
    return false;
}

void bfs(int x,int y,int z)//顺着代码duang一遍应该都能懂吧。。。
{
    queue<node>q;
    q.push(node(x,y,z));
    d[x][y][z]=0;
    while(q.size())
    {
        node qq=q.front();
        q.pop();
        if(G[qq.x][qq.y][qq.z]=='E')
        {
            cout<<"Escaped in "<<d[qq.x][qq.y][qq.z]<<" minute(s)."<<endl;
            return ;
        }
        for(int i=0;i<6;i++)
        {
            int xx=qq.x+dx[i];
            int yy=qq.y+dy[i];
            int zz=qq.z+dz[i];
            if(ok(xx,yy,zz))
            {
                q.push(node(xx,yy,zz));
                d[xx][yy][zz]=d[qq.x][qq.y][qq.z]+1;
            }
        }
    }
    cout<<"Trapped!"<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(cin>>L>>R>>C,L||R||C)
    {
        memset(G,0,sizeof(G));
        memset(d,-1,sizeof(d));
        sx=sy=sz=ex=ey=ez=0;//s为起点坐标,e为终点坐标
        for(int i=0;i<L;i++)
        {
            for(int j=0;j<R;j++)
            {
                for(int k=0;k<C;k++)
                {
                    cin>>G[j][k][i];
                    if(G[j][k][i]=='S')
                    {
                        sx=j;
                        sy=k;
                        sz=i;
                    }
                    else if(G[j][k][i]=='E')
                    {
                        ex=j;
                        ey=k;
                        ez=i;
                    }
                }
            }
        }
        bfs(sx,sy,sz);
    }
    return 0;
}

你可能感兴趣的:(B_Dungeon Master(POJ_2251))