HDU 1253 胜利大逃亡

题目有个坑是可能没有到达门口的路,结果WA好几次

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;

const int INF = 10000000;
int a, b, c, d;
int s[60][60][60], cost[60][60][60];
int dx[] = {0, 0, 1, -1, 0, 0}, dy[] = {0, 0, 0, 0, 1, -1}, dz[] = {-1, 1, 0, 0, 0, 0};
struct node
{
    int a, b, c;
};
void bfs()
{
    node e, p;
    queue <node> que;

    for(int i = 0; i < a; i++)
        for(int j = 0; j < b; j++)
            for(int k = 0; k < c; k++)
                cost[i][j][k] = INF;

    e.a = 0, e.b = 0, e.c = 0;
    que.push(e);
    cost[0][0][0] = 0;

    while(! que.empty())
    {
        e = que.front(); que.pop();
        if(e.a == a - 1 && e.b == b - 1 && e.c == c - 1)
        {
            if(cost[e.a][e.b][e.c] <= d)
            {
                printf("%d\n", cost[e.a][e.b][e.c]);
            }
            else printf("-1\n");
            return;
        }
        for(int i = 0; i < 6; i++)
        {
            int nx = e.a + dx[i], ny = e.b + dy[i], nz = e.c + dz[i];
            if(nx >= 0 && ny >= 0 && nz >= 0 && nx < a && ny < b && nz < c && s[nx][ny][nz] == 0 && cost[nx][ny][nz] == INF)
            {
                p.a = nx, p.b = ny, p.c = nz;
                que.push(p);
                cost[nx][ny][nz] = cost[e.a][e.b][e.c] + 1;
            }
        }
    }
    printf("-1\n");
}

int main()
{
    int t;

    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d%d%d", &a, &b, &c, &d);

        for(int i = 0; i < a; i++)
            for(int j = 0; j < b; j++)
                for(int k = 0; k < c; k++)
                    scanf("%d", &s[i][j][k]);
        bfs();
    }

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值