POJ2251 Dungeon Master (简单搜索)

本文介绍了一个三维迷宫问题,目标是从起点S到达终点E。通过使用广度优先搜索(BFS)算法来寻找最短路径,并提供了详细的代码实现。此外,还讨论了深度优先搜索(DFS)作为一种备选方案的时间效率问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【题目描述】
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!
题目大意是给你一个立体的空间,可以沿着‘.’走,问你能不能从‘S’ 走到 ‘E’。简单的bfs,和二维空间是一样的,只不过把四个方向变成了六个方向。
我原来是用dfs写的,结果超时,其实我认为这两个方式在时间复杂度上应当没有太大的区别。应该是我没有优化好。
dfs的代码我也没有删,只是没有调用
【代码】

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#define N 50
using namespace std;

struct T{
    int x;
    int y;
    int z;
    int de;
};

int dx[6] = {0,0,0,0,1,-1};
int dy[6] = {-1,1,0,0,0,0};
int dz[6] = {0,0,1,-1,0,0};
int f[N][N][N];
int g[N][N][N];
int a[N][N][N];
int n,m,k;
int i,j,l;
int zx,zy,zz,sx,sy,sz;
int ans;

void dfs(int x,int y,int z,int de){
    if (f[x][y][z] == 0) f[x][y][z] = N*N*N*N;
    if(de >= f[x][y][z]) return;
    f[x][y][z] = de;
    if (x == zx && y == zy && z == zz){
        ans = de;
    }
    for(int i = 0;i <= 6;i++){
        int xx = x + dx[i];
        int yy = y + dy[i];
        int zz = z + dz[i];
        if (xx<1 || xx>n || yy<1 || yy>m || zz<1 ||zz>k) continue;
        if (a[xx][yy][zz] == 0 || g[xx][yy][zz] == 1) continue;
        g[xx][yy][zz] = 1;
        dfs(xx,yy,zz,de+1);
        g[xx][yy][zz] = 0;
    }
}

int bfs(int x, int y, int z){
    queue<T> q;
    {
        T a;
        a.x = x;
        a.y = y;
        a.z = z;
        a.de = 0;
        q.push(a);
    }
    while(!q.empty()){
        T b = q.front();
        T c;
        for(int i = 0;i<6;i++){
            int xx = b.x + dx[i];
            int yy = b.y + dy[i];
            int tz = b.z + dz[i];
            if(xx == zx && yy == zy && tz == zz) {
                cout<<"Escaped in "<<(b.de + 1)<<" minute(s)."<<endl;
                return 1;
            }
            if (xx<1 || xx>n || yy<1 || yy>m || tz<1 ||tz>k) continue;
            if (a[xx][yy][tz] == 0 || g[xx][yy][tz] == 1) continue;
            g[xx][yy][tz] = 1;
            c.x = xx;c.y = yy;c.z = tz;c.de = b.de + 1;
            q.push(c);
        }
        q.pop();
    }
    return 0;
}

int main()
{
    string s;
    while(cin>>n>>m>>k && n && m && k){
        ans = 0;
        memset(a,0,sizeof(a));
        memset(f,0,sizeof(f));
        memset(g,0,sizeof(g));
        for(int i = 1;i <= n;i++){
            for(int j = 1;j <= m;j++){
                cin>>s;
                for(int l = 0;l < k;l++){
                    if (s[l] != '#') a[i][j][l+1] = 1;
                    if (s[l] == 'S') {
                        sx = i;
                        sy = j;
                        sz = l+1;
                    }
                    if (s[l] == 'E'){
                        zx = i;
                        zy = j;
                        zz = l+1;
                    }
                }
            }
        }
        g[sx][sy][sz] = 1;
    //  dfs(sx,sy,sz,0);
        if (bfs(sx,sy,sz) == 0) cout<<"Trapped!"<<endl;
        //else cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值