POJ-2251-Dungeon Master【BFS】【三维迷宫问题】

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!

题目链接:POJ-2251

题目大意:从S点到E点最近的距离,只能走‘.’

题目思路:类似于迷宫问题,不过这里是三维,搜6个方向即可

以下是代码:

//
// B.cpp
// 搜索
//
// Created by pro on 16/4/1.
// Copyright (c) 2016年 pro. All rights reserved.
//

#include <cstdio>
#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
int r,n,m;
string s[35][35];
struct node
{
    int x,y,z;
    int t;
};
node Begin,End;
queue <node> que;
int dx[6] = {0,0,0,0,1,-1};
int dy[6] = {0,0,-1,1,0,0};
int dz[6] = {1,-1,0,0,0,0};
int vis[35][35][35];
int bfs()
{
    que.push(Begin);
    vis[Begin.x][Begin.y][Begin.z] = 1;
    while(!que.empty())
    {
        node front = que.front();
        que.pop();
        if (front.x == End.x && front.y == End.y && front.z == End.z) return front.t;
        for (int i = 0; i < 6; i++)
        {
            node tmp;
            tmp.x = front.x + dx[i];
            tmp.y = front.y + dy[i];
            tmp.z = front.z + dz[i];
            tmp.t = front.t + 1;
            if (tmp.x >= 0 && tmp.x < r && tmp.y >= 0 && tmp.y < n && tmp.z >= 0 && tmp.z < m && !vis[tmp.x][tmp.y][tmp.z] && s[tmp.x][tmp.y][tmp.z] != '#')
            {
                que.push(tmp);
                vis[tmp.x][tmp.y][tmp.z] = 1;
            }
        }
    }
    return -1;
}
int main()
{
    while(cin >> r >> n >> m)
    {
        if (r == 0 && n == 0 && m == 0) break;
        memset(vis,0,sizeof(vis));
        while(!que.empty())
        {
            que.pop();
        }
        for (int i = 0; i < r; i++){
            string ret;
            for (int j = 0; j < n; j++){
                cin >> s[i][j];
                for (int k = 0; k < m; k++){
                    if (s[i][j][k] =='S'){
                        Begin.x = i,Begin.y = j,Begin.z = k;Begin.t = 0;
                    }
                    else if (s[i][j][k] == 'E'){
                        End.x = i,End.y = j,End.z = k;End.t= 0;
                    }
                }
            }
        }
        int ans = bfs();
        if (ans == -1){
            printf("Trapped!\n");
        }
        else{
            printf("Escaped in %d minute(s).\n",ans);
        }
    }

    return 0;
}

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