poj2251-dungeon master(bfs广搜)

复制题目过来的时候出现了不可调和的错误,又貼链接吧
poj2251

这个题目是平面迷宫的一个拓展问题,不过并没有实质性的突破,只是变成了三维的而已,多加了个上下楼。因此照着深搜的框架写就可以了。但我在写的时候出现了诸多bug,比如赋值的时候写了两个等号,标记走过的路线就失效了,把所有路线都往队列里压,结果就MLE了,题目给的数据范围是30,但讨论区里有人开到一百多才过,我的测试31就可以过了。

代码是老套路,不解释了

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
const int sz = 110;
char maze[sz][sz][sz];
int l, r, c;
int dir[6][3] = {{0,1,0},{0,0,1},{0,0,-1},{0,-1,0},{1,0,0},{-1,0,0}};

struct point
{   
    int x,
        y,
        z,
        step;
}now, next;

queue<point> q;

bool vtd[sz][sz][sz];
bool inside_x(int x)
{
    if(x >= 0 && x < l)
        return true;
    return false;
}
bool inside_y(int x)
{
    if(x >= 0 && x < r)
        return true;
    return false;
}bool inside_z(int x)
{
    if(x >= 0 && x < c)
        return true;
    return false;
}
int bfs(int x, int y, int z, int step)
{
    while(!q.empty())
        q.pop();
    now.x = x, now.y = y, now.z = z, now.step = step;
    q.push(now);
    vtd[x][y][z] = true;

    while(!q.empty())
    {
        now = q.front();
        q.pop();
        next = now;
        for(int i = 0; i < 6; i ++)
        {
            next.x = now.x + dir[i][0];
            next.y = now.y + dir[i][1];
            next.z = now.z + dir[i][2];
            if(maze[next.x][next.y][next.z] == '#' || !inside_x(next.x) || !inside_y(next.y) || !inside_z(next.z))
                continue;
            if(vtd[next.x][next.y][next.z])
            {
                continue;
            }
            if(maze[next.x][next.y][next.z] == 'E')
                return next.step + 1;
            next.step = now.step + 1;
            vtd[next.x][next.y][next.z] = true;
            q.push(next);
        }

    }
    return -1;
}

int main()
{
   // freopen("output.txt","r",stdin);
    while(scanf("%d%d%d", &l, &r, &c))
    {
        if(l == 0)
            break;
        int res;
        point start;
        int findStart = 1;
        memset(vtd,false,sizeof(vtd));
        memset(maze,0,sizeof(maze));
        for(int i = 0; i < l; i ++)
        {
            for(int j = 0; j < r; j ++)
            {
                scanf("%s", &maze[i][j]);
                if(findStart)
                    for(int k = 0; k < c; k ++)
                    {
                            if(maze[i][j][k] == 'S')
                            {
                                start.x = i;
                                start.y = j;
                                start.z = k;
                                findStart = 0;
                            }
                    }
            }
        }
        res = bfs(start.x,start.y,start.z,0);
        if(res > 0)
            printf("Escaped in %d minute(s).\n", res);
        else
            cout << "Trapped!" << endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值