poj 2251(简单BFS)

本文深入探讨了使用BFS算法解决三维迷宫逃脱问题的过程,包括算法的实现细节、优化策略以及样例输入输出分析。通过对比DFS与BFS的不同之处,作者不仅解决了迷宫逃脱的问题,还提供了宝贵的编程经验和教训。
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15359 Accepted: 5947

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


题目大意:一个三维的迷宫,要求走出迷宫的最少步数。如果无解,则输出Trapped!

这是我做的第二个BFS 题目,之前是想找DFS的。但是网上搜的分类竟然把这个题目分为深搜,然后我就傻逼的TLE了,当然在TLE之前我还经过无数遍的WA,甚至在编写代码的时候,连样例都过不了。。。。。后来被虐了N遍之后,才终于把它弄出来了,当然成就感也就瞬间爆表了。

解题思路:纯粹的BFS,当然别忘了,对于vis数组进行判断,否则还是会超时的。我就为此贡献了一个wa

为了纪念我的TLE,我还是贴一下我之前用DFS做的代码吧,吃一堑长一智嘛。

WA代码(DFS):

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
#ifdef __int64
typedef __int64 LL;
#else
typedef long long LL;
#endif
#define maxn 35
int vis[maxn][maxn][maxn];
char v[maxn][maxn][maxn];
int ans;
int l,r,c;
int bx,by,bz;
int dir[6][3]={0,-1,0,0,0,1,0,1,0,0,0,-1,1,0,0,-1,0,0};
void dfs(int z,int x,int y,int step)
{
    if(v[z][x][y]=='E')
    {
        ans=step;
        return ;
    }
    for(int i=0;i<6;i++)
    {
        int tempz=z+dir[i][0];
        int tempx=x+dir[i][1];
        int tempy=y+dir[i][2];
        if(tempz>=0&&tempz<l && tempx>=0&&tempx<r && tempy>=0&&tempy<c && !vis[tempz][tempx][tempy] && v[tempz][tempx][tempy]!='#')
        {
            z=z+dir[i][0];
            x=x+dir[i][1];
            y=y+dir[i][2];
            vis[z][x][y]=1;
            dfs(z,x,y,step+1);
            vis[z][x][y]=0;//切记做深搜的时候要记得恢复状态
            z=z-dir[i][0];
            x=x-dir[i][1];
            y=y-dir[i][2];
        }
    }
    return ;
}
int main()
{
    while(scanf("%d%d%d",&l,&r,&c)&&l&&r&&c)
    {
        for(int i=0;i<l;i++)
        {
            for(int j=0;j<r;j++)
            {
                scanf("%s",v[i][j]);
                for(int k=0;k<c;k++)
                {
                    if(v[i][j][k]=='S')
                    {
                        bz=i;
                        bx=j;
                        by=k;
                    }
                }
            }
        }
        ans=0;
        memset(vis,0,sizeof(vis));
        vis[bz][bx][by]=1;//起点肯定要进行标记的
        dfs(bz,bx,by,0);
        if(ans)
            printf("Escaped in %d minute(s).\n",ans);
        else
            printf("Trapped!\n");
    }
    return 0;
}


下面是AC代码(BFS):

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
#ifdef __int64
typedef __int64 LL;
#else
typedef long long LL;
#endif
#define maxn 50
int bz,bx,by;
char v[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int l,r,c;
int ans;
int dir[6][3]={0,-1,0,0,0,1,0,1,0,0,0,-1,1,0,0,-1,0,0};
struct point
{
    int x,y,z;
    int step;
};
void bfs(int z,int x,int y)
{
    queue<point>p;
    point t,q;
    t.x=x; t.y=y; t.z=z; t.step=0;
    vis[t.z][t.x][t.y]=1;
    p.push(t);
    while(!p.empty())
    {
        t=p.front();p.pop();
        if(v[t.z][t.x][t.y]=='E')
        {
            ans=t.step;
            return ;
        }
        for(int i=0;i<6;i++)
        {
            q.z=t.z+dir[i][0];
            q.x=t.x+dir[i][1];
            q.y=t.y+dir[i][2];
            q.step=t.step+1;
            if(q.z>=0&&q.z<l && q.x>=0&&q.x<r && q.y>=0&&q.y<c && v[q.z][q.x][q.y]!='#' && !vis[q.z][q.x][q.y])
            {
                vis[q.z][q.x][q.y]=1;
                p.push(q);
            }
        }
    }
}

int main()
{
    while(scanf("%d%d%d",&l,&r,&c)&&l&&r&&c)
    {
        for(int i=0;i<l;i++)
        {
            for(int j=0;j<r;j++)
            {
                scanf("%s",v[i][j]);
                for(int k=0;k<c;k++)
                {
                    if(v[i][j][k]=='S')
                    {
                        bz=i;
                        bx=j;
                        by=k;
                    }
                }
            }
        }
        memset(vis,0,sizeof(vis));
        ans=0;
        bfs(bz,bx,by);
        if(ans)
            printf("Escaped in %d minute(s).\n",ans);
        else
            printf("Trapped!\n");
    }
    return 0;
}



POJ2251是一个经典的题目,也被称为"水仙花的谜题"。该题目要求在一个三维的迷宫中找到从起点到终点的最短路径。 在这个题目中,迷宫由一个3D的数组表示,每个位置上的值代表了该位置的状态。其中,0表示可以通过的路径,1表示墙壁,2表示起点,3表示终点。你需要编写一个程序来找到从起点到终点的最短路径,并输出路径的长度。 解决这个问题的一种常见方法是使用广度优先搜索(BFS)算法。BFS算法可以从起点开始,逐层遍历迷宫中的位置,直到找到终点或者遍历完所有可达位置。在遍历过程中,你需要记录每个位置的距离和路径信息,以便找到最短路径。 以下是解决该问题的大致思路: 1. 定义一个队列,将起点加入队列,并标记起点已访问。 2. 使用循环来遍历队列中的元素,直到队列为空。 3. 在循环中,取出队列中的元素,并获取其相邻可达位置。 4. 对于每个相邻位置,判断是否为终点,如果是则输出最短路径长度并结束程序。 5. 如果不是终点,则判断该位置是否为可通过的路径,并且未被访问过。如果满足条件,则将该位置加入队列,并更新距离和路径信息。 6. 重复步骤2-5,直到找到终点或者遍历完所有可达位置。 这只是一个简单的介绍,实际解决该问题还需要考虑一些细节,比如如何表示迷宫、如何判断位置的合法性等。你可以在编写代码时参考相关的算法和数据结构知识。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值