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;
    }
  • 相关阅读:
    Win10系统的开机启动项如何去关闭?
    如何对Win10电脑文件夹选项进行设置?
    DNS缓存失败怎么解决?
    如何关闭Win10系统的时间轴功能?
    怎么处理Win7电脑打开软件速度慢的情况?
    Win10带有网络连接的安全模式怎么开启?
    如何解决Win10电脑网速慢的问题?
    【Beta】Scrum Meeting 5
    【Beta】Scrum Meeting 4
    【Beta】Scrum Meeting 3
  • 原文地址:https://www.cnblogs.com/keyboarder-zsq/p/5934590.html
Copyright © 2011-2022 走看看