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...
原文地址:https://www.cnblogs.com/xxx0624/p/2878842.html