搜索B Dungeon Master POJ - 2251

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhaohaibo_/article/details/85008574

Dungeon Master

#ACM/搜索#

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!
Source

BFS模版题,方向加了个上下。
无论广搜或深搜,走过的点是一定要标好的。不然会超时或超内存。

#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <map>
#define INF 0x3f3f3f3f
using namespace std;
typedef struct point{
    int l,r,c,step;
    point(){}
    point(int l,int r,int c,int step):l(l),r(r),c(c),step(step){}
}point;
int L, R, C; //L是迷宫层数,R,C分别为行和列
int Next[6][3] = {1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1};
point staPoint;
char map1[31][31][31];
int vis[31][31][31];
int bfs(){
    queue<point>que;
    que.push(staPoint);
    int nl, nr, nc;
    point head;
    while(!que.empty()){
        head = que.front();
        vis[head.l][head.r][head.c] = 1;
        que.pop();
        for(int k = 0; k < 6; k++){
            nl = head.l + Next[k][0];
            nr = head.r + Next[k][1];
            nc = head.c + Next[k][2];
            if(nl >= L || nr >= R || nc >= C || nl<0 || nr < 0 || nc < 0) continue;
            if(map1[nl][nr][nc] == 'E')
                return head.step + 1;
            if(map1[nl][nr][nc]=='.' && !vis[nl][nr][nc]){
                que.push(point(nl, nr, nc, head.step+1));
            }
        }
    }
    return 0;
}

int main(){
    ios::sync_with_stdio(false);
    while(true){
        memset(vis, 0, sizeof(vis));
        memset(map1, 0, sizeof(map1));
        cin >> L >> R >> C;
        if(L == 0) break;
        for(int l=0;l<L;l++){
            for(int i=0;i<R;i++){
                for(int j=0;j<C;j++){
                    cin >> map1[l][i][j];
                    if(map1[l][i][j] == 'S') {
                        staPoint = point(l,i,j,0);
                        vis[l][i][j] = 1;
                    }
                }
            }
        }
        int ans = bfs();
        if(ans)
            cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
        else
            cout<<"Trapped!"<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhaohaibo_/article/details/85008574