kuangbin专题一 简单搜索 地牢大师(POJ-2251)

发布时间 2023-04-14 20:12:25作者: Amαdeus

Dungeon Master

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 92499 Accepted: 31990

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!


题目大意

在一个三维矩阵中,找到起点到终点的最短距离。

在三维矩阵中,'.' 表示通路,'#' 表示障碍物;起始位置用 ”S” 表示,终点用 ”E” 表示。

每次可以向北,向南,向东,向西,向上或向下移动一个单元距离。不能走出边界范围。

输入格式

输入包含多组测试数据。

每组数据第一行包含三个整数 L,R,C
分别表示地牢层数,以及每一层地牢的行数和列数。

接下来是 L个 R行 C列的字符矩阵,用来表示每一层地牢的具体状况。

当输入一行为”0 0 0”时,表示输入终止。

输出格式

每组数据输出一个结果,每个结果占一行。



解题思路

这个题还是比较简单的,很容易看出是一个BFS求最短路的模型。只是升级到了三维矩阵的情况下,基本操作和二维矩阵下差不多,只要在原有的模板上加一些修改即可。

/*   一切都是命运石之门的选择  El Psy Kongroo  */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<functional>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<double, double> pdd;
typedef pair<string, pii> psi;
typedef __int128 int128;
#define x first
#define y second
const int inf = 0x3f3f3f3f, mod = 1e9 + 7;

const int N = 110;
char g[N][N][N];
int L, R, C;
int dist[N][N][N];  //最短步数
struct node{
    int x, y, z;
};
int sx, sy, sz, tx, ty, tz;       //起点 终点
int dx[6] = {1, -1, 0, 0, 0, 0};
int dy[6] = {0, 0, 1, -1, 0, 0};
int dz[6] = {0, 0, 0, 0, 1, -1};

int bfs(){
    if(sx == tx && sy == ty && sz == tz) return 0;
    memset(dist, -1, sizeof(dist));
    node src = {sx, sy, sz};
    queue<node> q; q.push(src);
    dist[sx][sy][sz] = 0;

    while(!q.empty()){
        int x = q.front().x, y = q.front().y, z = q.front().z;
        q.pop();
        for(int k = 0; k < 6; k ++ ){
            int nx = x + dx[k], ny = y + dy[k], nz = z + dz[k];
            if(nx < 0 || nx >= L || ny < 0 || ny >= R || nz < 0 || nz >= C) continue;
            if(g[nx][ny][nz] == '#' || dist[nx][ny][nz] != -1) continue;
            
            node cur; cur.x = nx, cur.y = ny, cur.z = nz;
            q.push(cur);
            dist[nx][ny][nz] = dist[x][y][z] + 1;

            if(nx == tx && ny == ty && nz == tz) return dist[nx][ny][nz];  //到达终点
        }
    }

    return -1;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    while(cin >> L >> R >> C && (L || R || C)){
        for(int i = 0; i < L; i ++ )
            for(int j = 0; j < R; j ++ )
                for(int k = 0; k < C; k ++ ){
                    cin >> g[i][j][k];
                    if(g[i][j][k] == 'S') sx = i, sy = j, sz = k;
                    if(g[i][j][k] == 'E') tx = i, ty = j, tz = k;
                }

        int res = bfs();

        if(res == -1) cout << "Trapped!" << endl;
        else cout << "Escaped in " << res << " minute(s)." << endl;
    }

    return 0;
}