poj 2251 Dungeon Master

本文探讨了在一个三维迷宫中寻找从入口到出口最短路径的问题,并提供了两种算法实现:深度优先搜索(DFS)与广度优先搜索(BFS)。通过对比,展示了BFS在解决此类问题上的优势。

大意:给定一个3D迷宫,已知出口和入口,求最小逃脱迷宫的时间。

思路1:DFS需要求最小路需要回溯,果断TLE。下附DFS代码:

 

 

void dfs(int x, int y, int z)
{
    if(!check(x, y, z) || flag[x][y][z] || maze[x][y][z] == '#'return ;
    else if(x == ex && y == ey && z == ez)
    {
        MIN <?= step;
        return ;
    }
    for(int i = 0; i < 6; i++)
    {
        int xx = x+dx[i];
        int yy = y+dy[i];
        int zz = z+dz[i];
        flag[xx][yy][zz] = 1;
        step++;
        printf("%d\n", step);
        dfs(xx, yy, zz);
        flag[xx][yy][zz] = 0;
        step--;                 //回溯时记得step--;
    }
}

 

BFS:

 

#include <stdio.h>
#include <stdlib.h>
#include <queue>
#include <string.h>
using namespace std;

const int SIZE = 31;

char maze[SIZE][SIZE][SIZE];
int flag[SIZE][SIZE][SIZE];
int l, r, c;
const int dx[] = {-1,1,0,0,0,0};
const int dy[] = {0,0,-1,1,0,0};
const int dz[] = {0,0,0,0,-1,1};
int bx, by, bz;   //Èë¿Ú 
int ex, ey, ez;   //³ö¿Ú 

int check(int x, int y, int z)
{
    if(x >= 0 && y >= 0 && z >= 0 && x < l && y < r && z < c && maze[x][y][z] != '#'return 1;
        return 0;
}


struct node
{
    int x, y, z;
    int step;
};


int bfs(int bx, int by, int bz)
{
    queue<node> Q;
    node p, q;
    p.x = bx; p.y = by; p.z = bz;
    p.step = 0;
    flag[bx][by][bz] = 1;
    Q.push(p);
    while(!Q.empty())
    {
        p = Q.front();
        Q.pop();
        if(p.x == ex && p.y == ey && p.z == ez)
        {
            printf("Escaped in %d minute(s).\n", p.step);
            return 1;
        }
        for(int i = 0; i < 6; i++)
        {
            q = p;
            q.x += dx[i];
            q.y += dy[i];
            q.z += dz[i];
            if(check(q.x, q.y, q.z) && !flag[q.x][q.y][q.z])
            {
                q.step++;
                flag[q.x][q.y][q.z] = 1;
                Q.push(q);
            }
        }
    }
    return 0;
}


void init()
{
    memset(flag, 0sizeof(flag));
    memset(maze, '#'sizeof(maze));
    ex = ey = ez = bx = by = bz = 0;
}

int main()
{
    int i, j, k;
    while(~scanf("%d%d%d", &l, &r, &c), l, r, c)
    {
        init();
        for(i = 0; i < l; i++)
        {
            for(j = 0; j < r; j++)
            {
                scanf("%s", maze[i][j]);
                for(k = 0; k < c; k++)
                {
                    if(maze[i][j][k] == 'E')
                    {
                        ex = i;
                        ey = j;
                        ez = k;
                    }
                    if(maze[i][j][k] == 'S')
                    {
                        bx = i;
                        by = j;
                        bz = k;
                    }
                }
            }
        }
        int ans = bfs(bx, by, bz);
        if(!ans) printf("Trapped!\n");
    }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/g0feng/archive/2012/09/02/2667381.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值