【POJ 2251 --- Dungeon Master】三维,BFS

【POJ 2251 --- Dungeon Master】三维,BFS

题目来源:点击进入【POJ 2251 — Dungeon Master】

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水题,只不过不同于常见的二维bfs,本题是一个三维图形,并且能走的方向有6个。
我们可以通过dir数组记录能走的6个方向。然后套bfs就行了。

AC代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define endl '\n'
typedef long long ll;
const int MAXN = 35;
char s[MAXN][MAXN][MAXN];
int vis[MAXN][MAXN][MAXN];
int l,r,c,sx,sy,sz,ex,ey,ez;
int dir[6][3]={{0,0,1},{0,0,-1},{-1,0,0},{1,0,0},{0,1,0},{0,-1,0}};

struct Node
{
    int x,y,z;
};

bool bfs()
{
    queue<Node> q;
    Node n;
    n.x=sx; n.y=sy; n.z=sz;
    q.push(n);
    vis[n.x][n.y][n.z]=1;
    while(!q.empty())
    {
        Node node=q.front();
        q.pop();
        for(int i=0;i<6;i++)
        {
            int dx=node.x+dir[i][0];
            int dy=node.y+dir[i][1];
            int dz=node.z+dir[i][2];
            if(dx<0 || dy<0 || dz<0 || dx>=l || dy>=r || dz>=c || vis[dx][dy][dz] || s[dx][dy][dz]=='#') continue;
            vis[dx][dy][dz]=vis[node.x][node.y][node.z]+1;
            if(dx==ex && dy==ey && dz==ez) return true;
            n.x=dx; n.y=dy; n.z=dz;
            q.push(n);
        }
    }
    return false;
}

int main()
{
    //SIS;
    while(cin >> l >> r >> c, l)
    {
        for(int i=0;i<l;i++)
            for(int j=0;j<r;j++)
                for(int k=0;k<c;k++)
                {
                    cin >> s[i][j][k];
                    if(s[i][j][k]=='S') { sx=i; sy=j; sz=k; }
                    else if(s[i][j][k]=='E') { ex=i; ey=j; ez=k; }
                }
        memset(vis,0,sizeof(vis));
        if(bfs()) cout << "Escaped in "<< vis[ex][ey][ez]-1 << " minute(s)." << endl;
        else cout << "Trapped!" << endl;
    }
    return 0;
}
发布了361 篇原创文章 · 获赞 127 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_41879343/article/details/104011329