poj 2251 Dungeon Master 题解

非常基本的BFS模板题,忘记了BFS模板的话就回来看看吧。

大致题意就是是一个三维的数组然后从入口找出口嘛!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
char map[31][31][31];
int c,l,r;
int is,js,ks;
int d[31][31][31] ;//到起点的距离的数组 bfs的作用本质上就是更新这个数组的过程哦 

 struct node{
    int nl;
    int nr;
    int nc;
  node() {}                  //如果没有这一句会直接无法定义node u 为什么? 因为有了构造函数吗
  node(int x,int y,int z) :nl(x), nr(y),nc(z) {}

};

node walk(const node&u,int turn)
{
    if(turn==1) return node(u.nl,u.nr,u.nc-1);   //向左走
    if(turn==2) return node(u.nl,u.nr,u.nc+1);   //向右走
    if(turn==3) return node(u.nl,u.nr-1,u.nc) ;  //向北走
    if(turn==4) return node(u.nl,u.nr+1,u.nc) ;  //向南走
    if(turn==5) return node(u.nl-1,u.nr,u.nc)  ; //向z轴的下走
    if(turn==6) return node(u.nl+1,u.nr,u.nc);   //向z轴的上走
 } 

void BFS()
{
    queue<node> q;
    memset(d,-1,sizeof(d));
     node u;
    u.nc=is;u.nl=js; u.nr=ks;
    d[u.nc][u.nl][u.nr]=0;
    q.push(u);
    while(!q.empty())
    {
        node u=q.front(); q.pop();
        if(map[u.nc][u.nl][u.nr]==‘E‘) { printf("%d\n",d[u.nc][u.nl][u.nr]); return;}                //这里没按题给的那个格式,这题太水了主要把思路捋一遍= =
        for(int i=1;i<=6;i++)                                                                       //对于每个点有六种路可以走
        {
            node v=walk(u,i);
            if(map[v.nc][v.nl][v.nr]!=‘#‘  && v.nc<c && v.nl<l && v.nr<r && d[v.nc][v.nl][v.nr]<0)   //判断是否能走的条件,能走就走
            {
                d[v.nc][v.nl][v.nr]=d[u.nc][u.nl][u.nr]+1;
                q.push(v);
             }
        }
     } 

}

int main()
{
    while(scanf("%d %d %d",&l,&r,&c)&&l&&r&&c)
    {
        while(l--)
        {
            for(int qi=0;qi<r;qi++)
            {

            scanf("%s",&map[qi]);

            }
        }
    int is,js,ks;                        //寻找初始点S
    for(int i=0;i<l;i++)
     for(int j=0;j<r;j++)
      for(int k=0;k<c;k++)
       if(map[i][j][k]==‘S‘)
       {
           is=i;
           js=j;
           ks=k;

       }

    BFS(); 

    }
}

那在最基本最基本的BFS中我们要注意的是什么呢?

1、BFS函数模板

BFS()

{

创建一个节点node u  各种初始化blabla;

u入node队列       //dfs用递归,bfs就用循环和队列啦

while(队列不空)

{

队列头拿出来,如果是答案就直接return了不是就走呗

for循环,每个结点有几种路就循环几次

node v=walk()       得到新节点

对该节点进行各种条件的判定(记得边界测定哦)

符合了就更新你的信息变量数组Blabla的

新节点入队列

}

2关于node结构体

要记得写构造函数哦事半功倍

然后要有node() {}

才能直接 node u这样去定义一个结构体变量

3

写walk函数或者也有用数组来写的也可以减少编程复杂度

emm好像也没什么别的了,最最基本的bfs模板就是这样啦,今天的任务也完成了(虽然是超级水题dont mind= =)

原文地址:https://www.cnblogs.com/hanny007/p/11576116.html

时间: 2024-10-18 04:20:00

poj 2251 Dungeon Master 题解的相关文章

POJ 2251 Dungeon Master(地牢大师)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层的地图,相同RC坐标处是相连通的.(.可走,#为墙) 解题思路:从起点开始分别往6个方向进行BFS(即入队),并记录步数,直至队为空.若一直找不到,则困住. /* POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路) */ #include <cstdio> #i

POJ 2251:Dungeon Master(三维BFS)

Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16178 Accepted: 6268 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 wit

BFS POJ 2251 Dungeon Master

题目传送门 1 /* 2 BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <queue> 8 using namespace std; 9 10 const int MAXN = 33; 11 const int INF = 0x3f3

POJ 2251 Dungeon Master(三维6方向BFS)

B - Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2251 Appoint description:  System Crawler  (2015-03-28) Description You are trapped in a 3D dungeon and need to find the quicke

poj 2251 Dungeon Master(优先队列bfs)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20867   Accepted: 8090 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

POJ 2251 Dungeon Master(bfs)

Dungeon Master 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 dow

POJ 2251 Dungeon Master (广搜)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18773   Accepted: 7285 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

POJ - 2251 - Dungeon Master (简单BFS)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20450   Accepted: 7917 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