[BFS]poj 2251 Dungeon Master

poj 2251 Dungeon Master

题意:

3D的迷宫,就是普通的迷宫又加了一维,相当于一个变成一个迷宫楼啦

给出起点和终点,如果可以从起点到终点,输出最小时间,过一个格或上楼下楼都是需要1min

思路:

一看到迷宫,还有最小值,没错啦BFS

没有难点,就是二维变三维了,照葫芦画瓢咯

代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
//notice the '#' can be placed.
using namespace std;
const int INF = 0x3fffffff;
const int MAXN = 30 + 5;

struct node{
    int le;
    int x;
    int y;
    int step;
}S, E;

int level, n, m;
char my_map[MAXN][MAXN][MAXN];
bool vis[MAXN][MAXN][MAXN];
int dle[] = {-1,1,0,0,0,0};
int dx[] = {0,0,-1,1,0,0};
int dy[] = {0,0,0,0,1,-1};

//can get the min value!
int BFS(int le, int x, int y, int step){
    queue  Q;
    Q.push(S);
    while(!Q.empty()){
        node Now, New;
        Now = Q.front();
        Q.pop();
        for(int i=0; i<6; i++){
            New.le = Now.le + dle[i];
            New.x = Now.x + dx[i];
            New.y = Now.y + dy[i];
            if(New.le<0 || New.le>=level) continue;
            if(New.x<0 || New.x>=n) continue;
            if(New.y<0 || New.y>=m) continue;
            if(my_map[New.le][New.x][New.y]=='#') continue;
            New.step = Now.step + 1;
            if(New.le==E.le && New.x==E.x && New.y==E.y)
                return New.step;
            if(!vis[New.le][New.x][New.y]){
                vis[New.le][New.x][New.y] = true;
                Q.push(New);
            }
        }
    }
    return -1;
}

int main(){
    while(scanf("%d%d%d", &level, &n, &m) && level && n &&m){
        getchar();
        int ans;
        memset(vis, false, sizeof(vis));
        for(int i=0; i


反思:

这样,简单的搜索题告一段落,接下来做记忆化搜索,反正我觉得就是dp啊……

你可能感兴趣的:(搜索)