首页 > 其他分享 >HDU 1253 胜利大逃亡

HDU 1253 胜利大逃亡

时间:2023-04-14 20:33:25浏览次数:40  
标签:HDU int 逃亡 60 ny nz cost 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;
}



标签:HDU,int,逃亡,60,ny,nz,cost,1253,&&
From: https://blog.51cto.com/u_4158567/6191062

相关文章

  • hdu 1272 小希的迷宫
    这是我第一次用并查集解决问题,其实刷这道题就是为了学习并查集,这是学长在hdu上找的模板题#include<iostream>#include<cstdio>usingnamespacestd;constintN=110000;intpar[N],mark[N];intfind1(intx){intr=x;while(par[r]!=r)r=par[r];......
  • HDU 5443 The Water Problem RMQ
    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5443题意:给定一个数组,查询区间最大值思路:RMQ模板题#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>usingnamespacestd;constint......
  • HDU 2894 DeBruijin (欧拉回路)
    题目地址:HDU2894跟POJ1392基本一样的。。代码如下:#include<iostream>#include<string.h>#include<math.h>#include<queue>#include<algorithm>#include<stdlib.h>#include<map>#include<set>#include<stdio.h>......
  • HDU 1116 && POJ 1386 Play on Words(欧拉路径)
    按字母来建边,每个单词的首字母和尾字母加边。先判断是否连通,然后判断每个字母的入度和出度不能出现差的绝对值大于2,然后入度和出度差的绝对值为1的不能超过两个。就可以形成欧拉路径代码如下:#include<iostream>#include<string.h>#include<math.h>#include<queue>#include......
  • HDU 1878 欧拉回路 (并查集+欧拉回路)
    题目地址:HDU1878这个题要注意欧拉回路与欧拉通路的区别。在都保证连通性的前提下,欧拉回路要求每个点的度数都是偶数,而欧拉通路允许两个点的度数是奇数。所以这题用并查集判断连通性后判断下度数就可以了。代码如下:#include<iostream>#include<string.h>#include<math.h>#in......
  • HDU 2089 不要62 (数位DP)
    简单的数位DP。代码如下:#include<iostream>#include<string.h>#include<math.h>#include<queue>#include<algorithm>#include<stdlib.h>#include<map>#include<set>#include<stdio.h>usingnamespacestd;#d......
  • HDU 2842 Chinese Rings(矩阵快速幂+递推)
    题目地址:HDU2842这个游戏是一个九连环的游戏。假设当前要卸下前n个环。由于要满足前n-2个都卸下,所以要先把前n-2个卸下,需要f(n-2)次。然后把第n个卸下需要1次,然后这时候要卸下第n-1个,然后此时前n-2个都已经被卸下了。这时候把前n-2个都卸下与都装上所需的次数是一样的,因为卸下与装......
  • HDU 1166 敌兵布阵(线段树)
    题目地址:HDU1166听说胡浩版的线段树挺有名的。于是就拜访了一下他的博客。详情戳这里。于是就完全仿照着胡浩大牛的风格写的代码。至于原理,鹏鹏学长已经讲的再清晰不过了。我就在下面的代码注释中将原理说明一下吧。来纪念第一发线段树。下面是代码+注释。#include<iostream>#in......
  • HDU - 7125 Master of Shuangpin
    D.MasterofShuangpintimelimitpertest1secondmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputAsyouknow,therearethreekindsofChineseinputmethodscommonlyused:Wubi,PinyinandShuangpin.WithShuangpin......
  • HDU 2222 Keywords Search (AC自动机)
    题目地址:HDU2222AC自动机第一发!真好奇这些算法是怎么被发明的。。算法的魅力真是无穷。这题是AC自动机模板题。自己实在写不出来,比着kuangbin的模板写的==代码如下:#include<iostream>#include<string.h>#include<math.h>#include<queue>#include<algorithm>#incl......