zoukankan      html  css  js  c++  java
  • HDU1253 (BFS)

    题意: bfs:从1,1,1到a,b,c的最短时间

    View Code
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 #include<queue>
     5 #include<algorithm>
     6 using namespace std;
     7 const int maxn = 55;
     8 const int inf = 9999999;
     9 int mat[ maxn ][ maxn ][ maxn ];
    10 const int dx[6]={0,-1,0,1,0,0};
    11 const int dy[6]={-1,0,1,0,0,0};
    12 const int dz[6]={0,0,0,0,1,-1};
    13 
    14 struct node{
    15     int x,y,z,t;
    16 }p,pp;
    17 int ans;
    18 
    19 void init( int a,int b,int c ){
    20     for( int i=1;i<=a;i++ )
    21         for( int j=1;j<=b;j++ )
    22             for( int k=1;k<=c;k++ ){
    23                 mat[ i ][ j ][ k ]=1;
    24             }
    25     ans=inf;
    26     return ;
    27 }
    28 
    29 void bfs( int a,int b,int c,int time ){
    30     p.x=1,p.y=1,p.z=1,p.t=0;
    31     queue<node>q;
    32     q.push( p );
    33     while( !q.empty() ){
    34         p=q.front(),q.pop();
    35         if( p.t>time ) continue;
    36         if( p.x==a&&p.y==b&&p.z==c ) ans=min( ans,p.t );
    37         for( int i=0;i<6;i++ ){
    38             pp.x=p.x+dx[ i ],pp.y=p.y+dy[ i ],pp.z=p.z+dz[ i ];
    39             if( pp.x<1||pp.x>a||pp.y<1||pp.y>b||pp.z<1||pp.z>c ) continue;
    40             if( mat[ pp.x ][ pp.y ][ pp.z ]==1 ) continue;
    41             mat[ pp.x ][ pp.y ][ pp.z ]=1;
    42             pp.t=p.t+1;
    43             q.push( pp );
    44         }
    45     }
    46 }
    47             
    48 int main(){
    49     int T;
    50     scanf("%d",&T);
    51     while( T-- ){
    52         int a,b,c,time;
    53         scanf("%d%d%d%d",&a,&b,&c,&time);
    54         init( a,b,c );
    55         for( int i=1;i<=a;i++ ){
    56             for( int j=1;j<=b;j++ ){
    57                 for( int k=1;k<=c;k++ ){
    58                     scanf("%d",&mat[ i ][ j ][ k ]);
    59                 }
    60             }
    61         }
    62         mat[ 1 ][ 1 ][ 1 ]=1;
    63         bfs( a,b,c,time );
    64         if( ans>time ) printf("-1\n");
    65         else printf("%d\n",ans);
    66     }
    67     return 0;
    68 }

     code2

    View Code
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 #include<queue>
     5 #include<algorithm>
     6 using namespace std;
     7 const int maxn = 55;
     8 const int inf = 99999;
     9 int mat[ maxn ][ maxn ][ maxn ],t[ maxn ][ maxn ][ maxn ];
    10 const int dx[6]={0,-1,0,1,0,0};
    11 const int dy[6]={-1,0,1,0,0,0};
    12 const int dz[6]={0,0,0,0,1,-1};
    13 
    14 struct node{
    15     int x,y,z,t;
    16 }p,pp;
    17 
    18 void init( int a,int b,int c ){
    19     for( int i=1;i<=a;i++ )
    20         for( int j=1;j<=b;j++ )
    21             for( int k=1;k<=c;k++ ){
    22                 mat[ i ][ j ][ k ]=1;
    23                 t[ i ][ j ][ k ]=inf;
    24             }
    25     return ;
    26 }
    27 
    28 void bfs( int a,int b,int c,int time ){
    29     t[ 1 ][ 1 ][ 1 ]=0;
    30     p.x=1,p.y=1,p.z=1,p.t=0;
    31     queue<node>q;
    32     q.push( p );
    33     while( !q.empty() ){
    34         p=q.front(),q.pop();
    35         if( p.t>time ) continue;
    36         for( int i=0;i<6;i++ ){
    37             pp.x=p.x+dx[ i ],pp.y=p.y+dy[ i ],pp.z=p.z+dz[ i ],pp.t=p.t+1;
    38             if( pp.x<1||pp.x>a||pp.y<1||pp.y>b||pp.z<1||pp.z>c ) continue;
    39             if( mat[ pp.x ][ pp.y ][ pp.z ]==1 ) continue;
    40             if( t[ pp.x ][ pp.y ][ pp.z ]>pp.t){
    41                 t[ pp.x ][ pp.y ][ pp.z ]=pp.t;
    42        
    43                 q.push( pp );
    44             }
    45         }
    46     }
    47 }
    48             
    49 int main(){
    50     int T;
    51     scanf("%d",&T);
    52     while( T-- ){
    53         int a,b,c,time;
    54         scanf("%d%d%d%d",&a,&b,&c,&time);
    55         init( a,b,c );
    56         for( int i=1;i<=a;i++ ){
    57             for( int j=1;j<=b;j++ ){
    58                 for( int k=1;k<=c;k++ ){
    59                     scanf("%d",&mat[ i ][ j ][ k ]);
    60                 }
    61             }
    62         }
    63         bfs( a,b,c,time );
    64         if( t[ a ][ b ][ c ]>time ) printf("-1\n");
    65         else printf("%d\n",t[ a ][ b ][ c ]);
    66     }
    67     return 0;
    68 }
    keep moving...
  • 相关阅读:
    欧拉回路 定理
    UESTC 1087 【二分查找】
    POJ 3159 【朴素的差分约束】
    ZOJ 1232 【灵活运用FLOYD】 【图DP】
    POJ 3013 【需要一点点思维...】【乘法分配率】
    POJ 2502 【思维是朴素的最短路 卡输入和建图】
    POJ 2240 【这题貌似可以直接FLOYD 屌丝用SPFA通过枚举找正权值环 顺便学了下map】
    POJ 1860【求解是否存在权值为正的环 屌丝做的第一道权值需要计算的题 想喊一声SPFA万岁】
    POJ 1797 【一种叫做最大生成树的很有趣的贪心】【也可以用dij的变形思想~】
    js 实现slider封装
  • 原文地址:https://www.cnblogs.com/xxx0624/p/2878842.html
Copyright © 2011-2022 走看看