zoukankan      html  css  js  c++  java
  • HDOJ搜索专题之胜利大逃亡

    BFS,走三维迷宫。

    View Code
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <queue>
     4 #define N 50
     5 using namespace std;
     6 typedef struct node
     7 {
     8   int x,y,z;
     9 }node;
    10 queue<node> Q;
    11 node cur,next;
    12 int dx[6]={1,-1,0,0,0,0};
    13 int dy[6]={0,0,1,-1,0,0};
    14 int dz[6]={0,0,0,0,1,-1};
    15 int g[N][N][N],t[N][N][N];
    16 int a,b,c,tmax,ans;
    17 void bfs()
    18 {
    19   bool success=false;
    20   memset(t,0xff,sizeof(t));
    21   cur.x=cur.y=cur.z=0;
    22   t[0][0][0]=0;
    23   while(!Q.empty()) Q.pop();
    24   Q.push(cur);
    25   while(!Q.empty() && !success)
    26   {
    27     cur=Q.front(),Q.pop();
    28     ans=t[cur.x][cur.y][cur.z];
    29     if(cur.x==a-1 && cur.y==b-1 && cur.z==c-1 && ans<=tmax) success=true;
    30     for(int d=0;!success && d<6;d++)
    31     {
    32       next=cur;
    33       next.x+=dx[d];
    34       next.y+=dy[d];
    35       next.z+=dz[d];
    36       if(next.x<0 || next.x>=a) continue;
    37       if(next.y<0 || next.y>=b) continue;
    38       if(next.z<0 || next.z>=c) continue;
    39       if(g[next.x][next.y][next.z]) continue;
    40       if(t[next.x][next.y][next.z]!=-1) continue;
    41       ans=t[next.x][next.y][next.z]=t[cur.x][cur.y][cur.z]+1;
    42       if(next.x==a-1 && next.y==b-1 && next.z==c-1 && ans<=tmax) success=true;
    43       else if(t[next.x][next.y][next.z]<tmax) Q.push(next);
    44     }
    45   }
    46   if(success) printf("%d\n",ans);
    47   else  puts("-1");
    48 }
    49 int main()
    50 {
    51   int T,i,j,k;
    52   scanf("%d",&T);
    53   while(T--)
    54   {
    55     scanf("%d%d%d%d",&a,&b,&c,&tmax);
    56     for(i=0;i<a;i++)
    57     {
    58       for(j=0;j<b;j++)
    59       {
    60         for(k=0;k<c;k++)  scanf("%d",&g[i][j][k]);
    61       }
    62     }
    63     bfs();
    64   }
    65   return 0;
    66 }
  • 相关阅读:
    URLEncode解决url中有特殊字符的问题
    监控系统概览
    SpringBoot 上传文件夹
    关于postgresql报 ERROR: XXX does not exist
    postgresql学习
    git学习
    学习博客
    面试准备
    jvm
    线程池面试题
  • 原文地址:https://www.cnblogs.com/algorithms/p/2504346.html
Copyright © 2011-2022 走看看