POJ - 2251 Dungeon Master (博客园的第一篇博客,奉献给一直没看过的BFS,)

(原创未经允许严禁转载!)欢迎评论探讨,有时间及时回复。渴求大佬来提出宝贵意见!

题目链接:

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

题目大意:

大体意思就是走迷宫,不过有很多层,就是普通迷宫问题多了两个方向。

解题思路:

dis数组设置方向dis[6][3]={1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1};

其余就像普通迷宫一样,队列,遍历;具体看代码。

代码:

 

#include<cstdio>
#include<iostream>
#include <cstring>
#include <queue>
#define N 5
using namespace std;
int n,m,l;
int dis[6][3]={1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1};//方向数组;
struct node
{
    int x,y,z,step;                              //step表示步数;
};
char map_[40][40][40];
node start,zhuanyi;             //start表示开始节点,zhuanyi表示位置移动。
int fx,fy,fz,ex,ey,ez;
int vis[100][100][100];    //标记作用数组;
int check(int x,int y,int z)         //检查是否越界或者碰墙
{
    if(x<0 || y<0 || z<0 || x>=n || y>=m || z>=l)
        return 1;
    else if(map_[x][y][z]== '#')
        return 1;
    else if(vis[x][y][z])
        return 1;
    return 0;
}
int bfs()
{
    start.x=fx;
    start.y=fy;
    start.z=fz;
    start.step=0;
    vis[start.x][start.y][start.z]=1;    //标记已经走过;
    queue<node >p;                         
    p.push(start);                       //起点入队列;
    while (!p.empty())
    {
        start=p.front();                                 //以此节点为中心进行遍历;
        p.pop();
        if(start.x == ex && start.y == ey && start.z == ez) //终止条件如果能走到终点返回;
            return start.step;
        for (int i=0; i<6; i++)
        {
                zhuanyi.x=start.x+dis[i][0];
                zhuanyi.y=start.y+dis[i][1];
                zhuanyi.z=start.z+dis[i][2];                            //位置移动;
                if (check(zhuanyi.x,zhuanyi.y,zhuanyi.z)) continue;     //越界就换方向
                else
                {
                    vis[zhuanyi.x][zhuanyi.y][zhuanyi.z]=1;             //标记已经走过;
                    zhuanyi.step=start.step+1;                            //步数+1;
                    p.push(zhuanyi);                                     //入队列;
                }
        }
    }
    return 0;
}
int main()
{
    while (cin>>n>>m>>l&&(n+m+l))
    {
        for (int i=0; i<n; i++)
        {
            for (int j=0; j<m; j++)
            {
                for (int k=0; k<l; k++)
                {
                    cin>>map_[i][j][k];
                    if (map_[i][j][k]=='S')
                    {
                        fx=i;
                        fy=j;
                        fz=k;
                    }
                    if (map_[i][j][k]=='E')
                    {
                        ex=i;
                        ey=j;
                        ez=k;
                    }
                }
            }
        }
        memset(vis,0,sizeof(vis));
        int ans=0;     //标记变量ans,如果是0表示到不了终点,能到达就是实际最短步数;
        ans=bfs();
        if (ans) cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
        else cout<<"Trapped!"<<endl;
    }
}
View Code

 

PS:

如果这题理解比较困难,建议先做一下两道题目。对BFS有大体的了解;

poj3984--迷宫问题(输出最短路径BFS)   :http://poj.org/problem?id=3984

HRBUST - 1143  泉水 (VJ地址): https://vjudge.net/problem/HRBUST-1143

 

posted @ 2017-05-01 21:27  飞将-奉先  阅读(170)  评论(0编辑  收藏  举报