题目表述:http://acm.hdu.edu.cn/showproblem.php?pid=1253

单纯的多了两个方向的bfs而已,纯当找手感。(然而也改了好几次...

 1 #include<cstring>
 2 #include<cstdio>
 3 using namespace std;
 4 struct node{
 5     int a,b,c;
 6 }q[50005];
 7 int K,mp[55][55][55],head,tail,A,B,C,T,f[55][55][55],flag;
 8 int da[6]={1,-1,0,0,0,0};
 9 int db[6]={0,0,1,-1,0,0};
10 int dc[6]={0,0,0,0,1,-1};
11 int main(){
12     scanf("%d",&K);
13     while(K--){
14         memset(mp,0,sizeof mp);
15         memset(f,0,sizeof f);
16         flag = 0;
17         int a,b,c,o;
18         scanf("%d%d%d%d",&A,&B,&C,&T);
19         for(int i=0;i<A;++i){
20             for(int j=0;j<B;++j){
21                 for(int k=0;k<C;++k){
22                     scanf("%d",&o);
23                     mp[i][j][k] = o;
24                 }
25             }
26         }
27         if(mp[A-1][B-1][C-1]){
28             printf("-1\n");
29             continue;
30         }
31         q[1].a = q[1].b = q[1].c = 0;
32         head = tail = 1;
33         f[0][0][0] = 1;
34         while(head <= tail){
35             a = q[head].a;  b = q[head].b;  c = q[head].c;
36             for(int i=0;i<6;++i){
37                 int na,nb,nc;
38                 na = a + da[i]; nb = b + db[i]; nc = c + dc[i];
39                 if(na>=0 && na<A && nb>=0 && nb<B && nc>=0 && nc<C){
40                     if(!f[na][nb][nc] && !mp[na][nb][nc] && A+B+C-na-nb-nc-3<=T){
41                         f[na][nb][nc] = f[a][b][c] + 1;
42                         if(f[na][nb][nc]>=T)    flag = 1;
43                         q[++tail].a = na;   q[tail].b = nb;  q[tail].c = nc;
44                     }
45                 }
46             }
47             head++;
48             if(f[A-1][B-1][C-1] || flag)    break;
49         }
50         if(!f[A-1][B-1][C-1]||f[A-1][B-1][C-1]>=T){
51             printf("-1\n");
52         }
53          else   printf("%d\n",f[A-1][B-1][C-1]-1);
54     }
55     return 0;
56 }