一题简直模板的
BFS,只是三维遍历而已。

#include <stdio.h>

#include <iostream>

#include <sstream>

#include <string.h>

#include <math.h>

#include<stdlib.h>

#include <queue>

#include <set>

#include <algorithm>

using namespace std;

int A,B,C,t;

int dxy[7][3]=

{

    0,-1,0,

    0,1,0,

    1,0,0,

    -1,0,0,

    0,0,1,

    0,0,-1

};

int a[55][55][55];

int flag[55][55][55];

struct asd

{

    int x,y,z;

    int step;

} now,ne;

queue<asd>q;

void bfs()

{

    int i;

    memset(flag,0,sizeof(flag));

    while(!q.empty())

        q.pop();

    now.x=0;

    now.y=0;

    now.z=0;

    now.step=0;

    flag[0][0][0]=1;

    q.push(now);

    while(!q.empty())

    {

        now=q.front();

        q.pop();

        if(now.step+1>t)

            break;

        for(i=0; i<6; i++)

        {

            int dx=now.x+dxy[i][0];

            int dy=now.y+dxy[i][1];

            int dz=now.z+dxy[i][2];

            if(dx==A-1&&dy==B-1&&dz==C-1&&now.step+1<=t)

            {

                printf("%d\n",now.step+1);

                return;

            }

            if(dx<0||dy<0||dz<0||dz>=C||dy>=B||dx>=A||flag[dx][dy][dz]||a[dx][dy][dz])

                continue;

            flag[dx][dy][dz]=1;

            ne.x=dx;

            ne.y=dy;

            ne.z=dz;

            ne.step=now.step+1;

            q.push(ne);

        }

    }

    printf("-1\n");

}

int main()

{

    int i,j,k,T;

    scanf("%d",&T);

    while(T--)

    {

        scanf("%d%d%d%d",&A,&B,&C,&t);

        for(i=0; i<A; i++)

        {

            for(j=0; j<B; j++)

            {

                for(k=0; k<C; k++)

                    scanf("%d",&a[i][j][k]);

            }

        }

        bfs();

    }

    return 0;

}

hdoj1253的更多相关文章

  1. HDOJ1253 胜利大逃亡 BFS

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...

随机推荐

  1. 辛星浅析html5中的role属性

    我们使用role属性告诉辅助设备.这个元素所扮演的角色.比方点击的按钮,我们通常就使用role="button",会让这个元素可点击. 可是它很多其它的是用来增强语义性,当现有的h ...

  2. Presenting view controllers on detached view controllers is discouraged

    本文转载至 http://www.xuebuyuan.com/2117943.html Presenting view controllers on detached view controllers ...

  3. SAM4E单片机之旅——12、USART

    清楚了UART的用法之后,现在来研究一下USART的用法.和上一次差不多,这次也通过USART的串口来实现和PC的通信.和上一次不同的是,USART本身就有接收超时的功能,所以这次就不用TC了. US ...

  4. ssemble JavaBeans components into an application without having to write any code

    https://docs.oracle.com/javase/tutorial/javabeans/ https://docs.oracle.com/javase/tutorial/javabeans ...

  5. java基础汇总

    1.关于Http和Hibernatet里面Session的区别HttpSession      HttpSession:是一个抽象接口,J2EE的Web程序在运行的时候,会给每一个新的访问者建立一个H ...

  6. python:将字典转化为数据框

    my_dict = {,,} import pandas as pd pd.Series(my_dict) fuck i you dtype: int64 一个key只有一个value的字典如果直接转 ...

  7. PHP自定义网站根目录

    1.打开httpd.conf配置文件(xampp下apache文件夹中的conf里) Ctrl + F 查找documentroot 找到结果 改好后保存,并重启apache

  8. 【C/C++】malloc()

    <math.h>文件中对malloc()函数原型: _CRTIMP void *  __cdecl malloc(size_t); MSDN中对malloc()的解释: malloc re ...

  9. RAM的分类

    转载自:http://wenku.baidu.com/view/b17d73244b35eefdc8d333ab.html RAM(随机存储器)可以分为SRAM(静态随机存储器)和DRAM(动态随机存 ...

  10. linux内存占用分析

    概述 想必在linux上写过程序的同学都有分析进程占用多少内存的经历,或者被问到这样的问题——你的程序在运行时占用了多少内存(物理内存)?通常我们可以通过top命令查看进程占用了多少内存.这里我们可以 ...