kuangbin专题一B题:POJ2251:Dungeon Master

POJ2251:Dungeon Master

kuangbin专题一B题

题目链接:http://poj.org/problem?id=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广度优先搜索适合求最优解 的情况
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std ; 

#define maxn 50
#define LL long long 
int turnx[6] = {0 , 0 , 1 , -1 , 0 , 0,} ; // 上下左右前后 六个方向坐标变换 
int turny[6] = {0 , 0 , 0 , 0 ,  1 , -1} ; 
int turnz[6] = {1 , -1 , 0 , 0 , 0 , 0 } ; 

char map[maxn][maxn][maxn] ; 
bool visit[maxn][maxn][maxn] ; 
int R, L , C ;  
LL result ; 

struct node {
    int x ; 
    int y ; 
    int z ; 
    int step ; 
};
node st , es ; 

bool check(node w){//检查下一步要走的地方是否符合条件 
    if(!visit[w.x][w.y][w.z]&&map[w.x][w.y][w.z]!='#'&&w.x>=1&&w.x<=R&&w.y>=1&&w.y<=L&&w.z>=1&&w.z<=C)
        return true ; 
    return false ; 
}

void BFS(){
    queue<node>Q ; 
    Q.push(st) ; 
    
    while(!Q.empty()){
        node q = Q.front() ; 
        Q.pop() ; 
        if(q.x==es.x&& q.y==es.y&&q.z==es.z){
            result = q.step ; 
            return;
        }
        node turn ; 
        for(int i=0 ; i<6 ; i++){//向六个方向探测 
            turn.x = q.x + turnx[i] ; 
            turn.y = q.y + turny[i] ; 
            turn.z = q.z + turnz[i] ; 
            turn.step = q.step + 1 ; 
            if(check(turn)){
                visit[turn.x][turn.y][turn.z] = 1 ; 
                Q.push(turn) ; 
            }
        }
        
    }
}


int main(){
    
    while(~scanf("%d%d%d" , &R , &L , &C)){
        if(R==0 && L==0 && C==0){
            break; 
        } 
        result = 0 ; 
        memset(visit , 0 , sizeof(visit)) ; 
        
        for(int i=1 ; i<=R ; i++){
            for(int j=1 ; j<= L ; j++){
                for(int k=1 ; k<=C ; k++){
                    scanf(" %c" , &map[i][j][k]) ; 
                    if(map[i][j][k] == 'S'){
                        st.x = i ; 
                        st.y = j ; 
                        st.z = k ; 
                        st.step = 0 ; 
                        visit[i][j][k] = 1 ; 
                    }
                    if(map[i][j][k] == 'E'){
                        es.x = i ; 
                        es.y = j ; 
                        es.z = k ; 
                    } 
                }
            }
        }
        
        BFS() ; 
        if(result == 0 ){
            printf("Trapped! 
") ; 
        } else if(result == 1 ){
            printf("Escaped in %d minute.
" , result) ; 
        } else {
            printf("Escaped in %d minute(s).
" , result) ; 
        }
    }
    return 0 ; 
} 
原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/7637795.html