zoukankan      html  css  js  c++  java
  • hdoj1253

    一题简直模板的
    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 ",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 ");
    }

    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;
    }
  • 相关阅读:
    java线程安全单例
    ConcurrentHashMap 中putIfAbsent 和put的区别
    Google Guava -缓存cache简单使用
    Jstorm TimeCacheMap源代码分析
    poj 3277...离散化+线段树...
    spoj 1716...动态区间的最大连续子段和问题...点修改...
    spark
    hdu 1754...忘了个getchar(),蛋疼了半天...原来划水也有蛋疼的时候...
    hdu 1556...线段树划水...
    PHP学习笔记06——面向对象版图形计算器
  • 原文地址:https://www.cnblogs.com/keyboarder-zsq/p/5934590.html
Copyright © 2011-2022 走看看