UVA 532 & POJ 2251 Dungeon Master (BFS)

本文介绍了一个基于广度优先搜索(BFS)的3D迷宫逃脱算法实现,该算法用于寻找从起点到终点的最短路径。文章提供了一段完整的C++代码示例,并详细解释了如何遍历迷宫、标记已访问区域以及如何判断是否找到出口。

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

http://poj.org/problem?id=2251

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=473


Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15809 Accepted: 6138

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



给出一个三维(L×R×C)的地图,求出从S到E的最少步数,其中'#' 是障碍,'.'可走。

直接BFS,注意标记访问


#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<cctype>
#include<cmath>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<map>
#include<set>
#define sqr(x) ((x)*(x))
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define mm

using namespace std;

int vis[33][33][33];
int fx,fy,fz,tx,ty,tz;
int L,R,C;
int f,r;

int dx[]={0,0,0,0,1,-1};
int dy[]={0,0,1,-1,0,0};
int dz[]={1,-1,0,0,0,0};

struct record
{
    int x,y,z,t;
    record(int xx=0,int yy=0,int zz=0,int tt=0)
    {
        x=xx;y=yy;z=zz;t=tt;
    }
    record(const record &r)
    {
        x=r.x;y=r.y;z=r.z;t=r.t;
    }
}q[33333];

int BFS()
{
    while (f<=r)
    {
        record n(q[f++]);

        //printf("%d %d %d %d\n",n.x,n.y,n.z,n.t);

        if (n.x==tx && n.y==ty && n.z==tz)   return n.t;


        int x,y,z;

        for (int i=0;i<6;i++)
        {
            x=n.x+dx[i];
            y=n.y+dy[i];
            z=n.z+dz[i];

            if (vis[x][y][z]!=-1)
            {
                vis[x][y][z]=-1;
                q[++r]=record(x,y,z,n.t+1);
            }
        }
    }

    return -1;
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("/home/fcbruce/文档/code/t","r",stdin);
    #endif // ONLINE_JUDGE

    while (scanf("%d%d%d",&L,&R,&C),L||R||C)
    {
        char c;
        memset(vis,-1,sizeof(vis));
        for (int i=1;i<=L;i++)
        {
            getchar();
            for (int j=1;j<=R;j++)
            {
                for (int k=1;k<=C;k++)
                {
                    c=getchar();
                    if (c=='#') continue;
                    vis[i][j][k]=0;
                    if (c=='S')
                    {
                        fx=i;fy=j;fz=k;
                    }
                    if (c=='E')
                    {
                        tx=i;ty=j;tz=k;
                    }
                }
                getchar();
            }
        }

        //printf("%d %d %d %d\n",fx,fy,fz,0);

        q[f=r=0]=record(fx,fy,fz,0);
        vis[fx][fy][fz]=-1;
        int k=BFS();
        if (~k)
        {
            printf("Escaped in %d minute(s).\n",k);
        }
        else
        {
            printf("Trapped!\n");
        }


    }


    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值