• hdu1253胜利大逃亡(BFS)


    http://acm.hdu.edu.cn/showproblem.php?pid=1253

    View Code
     1 #include<stdio.h>
     2 #include<string.h>
     3 int a[51][51][51];
     4 struct node
     5 {
     6     int x,y,z,num;
     7 }q[50001];
     8 int p,d,f[51][51][51];
     9 int an[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
    10 int main()
    11 {
    12     int i,j,k,n,m,z,w,t,te;
    13     scanf("%d", &t);
    14     while(t--)
    15     {
    16         memset(f,0,sizeof(f));        
    17         scanf("%d %d %d %d",&n,&m,&z,&te);
    18         for(i = 1; i <= n ; i++)        
    19             for(j = 1; j <= m ; j++)
    20                 for(k = 1; k <= z ; k++)
    21                     scanf("%d",&a[i][j][k]);        
    22         if(n==1&&m==1&&z==1)
    23         {
    24             printf("0\n");
    25             continue;
    26         }    
    27         if(a[n][m][z]==1)
    28         {
    29             printf("-1\n");
    30             continue;
    31         }    
    32         int flag = 0;
    33         p = 0;
    34         d = 1;
    35         q[d].x = 1;
    36         q[d].y = 1;
    37         q[d].z = 1;
    38         q[d].num = 0;
    39         while(p!=d)
    40         {
    41             p++;
    42             if(q[p].num>te)//剪枝 大于时间退出 
    43                 break;
    44             if(q[p].z==z&&q[p].x==n&&q[p].y==m&&q[p].num<=te)
    45             {
    46                 flag = 1;
    47                 break;
    48             }        
    49             for(i = 0 ;i < 6 ; i++)
    50             {
    51                 int tx = q[p].x+an[i][0];
    52                 int ty = q[p].y+an[i][1];
    53                 int tz = q[p].z+an[i][2];
    54                 if(tx>0&&tx<=n&&ty>0&&ty<=m&&tz>0&&tz<=z&&!f[tx][ty][tz]&&a[tx][ty][tz]==0)
    55                 {
    56                     f[tx][ty][tz] = 1;
    57                     q[++d].x = tx;
    58                     q[d].y = ty;
    59                     q[d].z = tz;
    60                     q[d].num = q[p].num+1;
    61                 }
    62             }
    63         }
    64         if(flag)
    65             printf("%d\n",q[p].num);
    66         else
    67             printf("-1\n");
    68     }
    69     return 0;
    70 }
  • 相关阅读:
    3Sum
    Longest Common Prefix
    Integer to Roman
    Roman to Integer
    Container With Most Water
    String to Integer (atoi)
    Regular Expression Matching
    codeforces-873C. Strange Game On Matrix[模拟]
    hdu-5927 Auxiliary Set
    Codeforces-869 C. The Intriguing Obsession [组合数学]
  • 原文地址:https://www.cnblogs.com/shangyu/p/2618712.html
Copyright © 2020-2023  润新知