poj2251 三维bfs求最短距离

本文介绍了一种解决三维迷宫问题的BFS算法,通过输入描述三维迷宫的布局,输出从起点到终点的最短路径所需的时间,或者判断是否无法逃脱。详细解释了算法的实现过程,包括如何遍历三维空间,处理边界条件和优化路径查找。

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


Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 22466 Accepted: 8769

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<bitset>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

inline int in()
{
    int res=0;char c;
    while((c=getchar())<'0' || c>'9');
    while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
    return res;
}
struct st
{
    int x,y,z;
};
const int N=100100;
char a[33][33][33]; //层数 x y
int dis[33][33][33];
int l,n,m;
int dx[]={-1,0,1,0},dy[]={0,-1,0,1};

int bfs(st start,st end)
{
    queue<st> q;
    q.push(start);
    dis[start.x][start.y][start.z]=0;
    while(!q.empty())
    {
        st now = q.front();
        q.pop();
        if(now.x==end.x && now.y==end.y && now.z==end.z) return dis[end.x][end.y][end.z];

        for(int i=0;i<4;i++)
        {
            int nk=now.x;
            int nx=now.y+dx[i];
            int ny=now.z+dy[i];
            if(nk>=0 && nx>=0 && ny>=0 && nk<l && nx<n && ny<m && a[nk][nx][ny]=='.' && dis[nk][nx][ny]==inf)
            {
                dis[nk][nx][ny]=dis[now.x][now.y][now.z]+1;
                q.push(st{nk,nx,ny});
            }
        }
        int nk=now.x+1;
        int nx=now.y;
        int ny=now.z;
        if(nk>=0 && nk<l && a[nk][nx][ny]=='.' && dis[nk][nx][ny]==inf)
        {
            dis[nk][nx][ny]=dis[now.x][now.y][now.z]+1;
            q.push(st{nk,nx,ny});
        }
        nk=now.x-1;
        nx=now.y;
        ny=now.z;
        if(nk>=0 && nk<l && a[nk][nx][ny]=='.' && dis[nk][nx][ny]==inf)
        {
            dis[nk][nx][ny]=dis[now.x][now.y][now.z]+1;
            q.push(st{nk,nx,ny});
        }

    }
    return dis[end.x][end.y][end.z];
}
int main()
{
    while(~scanf("%d%d%d",&l,&n,&m) && l)
    {
        for(int k=0;k<l;k++)
        {
            for(int i=0;i<n;i++) scanf("%s",a[k][i]);
        }
        mem(dis,inf);
        st start,end;
        for(int k=0;k<l;k++)
        {
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    if(a[k][i][j]=='S')
                    {
                        start=st{k,i,j};

                    }
                    else if(a[k][i][j]=='E')
                    {
                        end=st{k,i,j};
                        a[k][i][j]='.';//注意要把end位置设置为'.',要不然会找不到,这里错了好几次
                    }
                }
            }
        }
        int ans=bfs(start,end);

        if(ans==inf) puts("Trapped!");
        else
            printf("Escaped in %d minute(s).\n",ans);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值