NYOJ 353 3D dungeon + zoj 1940 Dungeon Master

103 篇文章 1 订阅
44 篇文章 0 订阅

题目来源:http://acm.nyist.net/JudgeOnline/problem.php?pid=353

zoj:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=940

三维广搜,其实跟二维的一样,注意方向。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

const int MAXN = 35;
const int INF = 0xfffffff;

struct Node
{
    int x, y, z;
    int step;
    Node()
    {
        x = y = z = 0;
        step = 0;
    }
};

Node TargetPos;
char Graph[MAXN][MAXN][MAXN];
//int MinTime[MAXN][MAXN][MAXN];
int row, col, level;

int dir[6][3] = {{1, 0, 0}, {0, 1, 0}, {-1, 0, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};


bool Is_CanGo(Node CurPos)
{
    if(CurPos.x < 0 || CurPos.x >= level || CurPos.y < 0 || CurPos.y >= row || CurPos.z < 0 || CurPos.z >= col || Graph[CurPos.x][CurPos.y][CurPos.z] == '#')
        return false;
    return true;
}

int BFS(Node S)
{
    queue <Node> Que;
    Que.push(S);
    while(!Que.empty())
    {
        Node Curpos = Que.front();
        Que.pop();


        if(Curpos.x == TargetPos.x && Curpos.y == TargetPos.y && Curpos.z == TargetPos.z)
            return Curpos.step;
        for(int i = 0; i < 6; ++i)
        {
            Node t;
            t.x = Curpos.x + dir[i][0];
            t.y = Curpos.y + dir[i][1];
            t.z = Curpos.z + dir[i][2];
            t.step = Curpos.step + 1;
            if(Is_CanGo(t))
            {
                Que.push(t);
                Graph[t.x][t.y][t.z] = '#';
            }
        }
    }
    return -1;
}

int main()
{
    while(scanf("%d %d %d", &level, &row, &col) && (level + row + col))
    {
        Node StartPos;
        for(int i = 0; i < level; ++i)
        {
            for(int j = 0; j < row; ++j)
            {
                scanf("%s", Graph[i][j]);
                for(int z = 0; z < col; ++z)
                {
                    if(Graph[i][j][z] == 'S')
                        StartPos.x = i, StartPos.y = j, StartPos.z = z;
                    else if(Graph[i][j][z] == 'E')
                        TargetPos.x = i, TargetPos.y = j, TargetPos.z = z;
                }
                getchar();
            }
           if(i != level - 1)
                getchar();
        }
        StartPos.step = 0;
        int t;
        t = BFS( StartPos );
        if(t != -1)
            printf("Escaped in %d minute(s).\n", t);
        else
            printf("Trapped!\n");
    }
    return 0;
}

写完之后,总感觉这个代码有问题,事实上是存在问题的!这个代码中当跳出BFS时,返回的结果不一定是最短时间!较为弱的数据或许能过,但是强的数据是过不了的!

现补上另一种正确的写法!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

const int MAXN = 35;
const int INF = 0xfffffff;

struct Node
{
    int x, y, z;
    int step;
    Node()
    {
        x = y = z = 0;
        step = 0;
    }
};

Node TargetPos;
char Graph[MAXN][MAXN][MAXN];
int MinTime[MAXN][MAXN][MAXN];
int row, col, level;

int dir[6][3] = {{1, 0, 0}, {0, 1, 0}, {-1, 0, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};


bool Is_CanGo(Node CurPos)
{
    if(CurPos.x < 0 || CurPos.x >= level || CurPos.y < 0 || CurPos.y >= row || CurPos.z < 0 || CurPos.z >= col || Graph[CurPos.x][CurPos.y][CurPos.z] == '#')
        return false;
    return true;
}

void BFS(Node S)
{
    queue <Node> Que;
    Que.push(S);
    while(!Que.empty())
    {
        Node Curpos = Que.front();
        Que.pop();

        for(int i = 0; i < 6; ++i)
        {
            Node t;
            t.x = Curpos.x + dir[i][0];
            t.y = Curpos.y + dir[i][1];
            t.z = Curpos.z + dir[i][2];

            if(Is_CanGo(t))
            {
                t.step = Curpos.step + 1;
                if(t.step < MinTime[t.x][t.y][t.z])
                {
                    MinTime[t.x][t.y][t.z] = t.step;
                    Que.push(t);
                }
            }
        }
    }
}

int main()
{
    while(scanf("%d %d %d", &level, &row, &col) && (level + row + col))
    {
        Node StartPos;
        for(int i = 0; i < level; ++i)
        {
            for(int j = 0; j < row; ++j)
            {
                scanf("%s", Graph[i][j]);
                for(int z = 0; z < col; ++z)
                {
                    MinTime[i][j][z] = INF;
                    if(Graph[i][j][z] == 'S')
                        StartPos.x = i, StartPos.y = j, StartPos.z = z;
                    else if(Graph[i][j][z] == 'E')
                        TargetPos.x = i, TargetPos.y = j, TargetPos.z = z;
                }
                getchar();
            }
           if(i != level - 1)
                getchar();
        }
        StartPos.step = 0;
        BFS( StartPos );
        int t;
        t = MinTime[TargetPos.x][TargetPos.y][TargetPos.z];
        if(t != INF)
            printf("Escaped in %d minute(s).\n", t);
        else
            printf("Trapped!\n");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值