HDU 胜利大逃亡 1253 BFS

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

三维的BFS,跟地下城那道题很像。不用怎么解释。题目上说的很清楚。

View Code
 1 #include <stdio.h>
 2 #include <string.h>
 3 struct node
 4 {
 5     int x,y,z;
 6     int t;
 7 }q[1000005];
 8 int map[55][55][55];
 9 int pro[55][55][55],f,r;
10 int to[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
11 
12 int main()
13 {
14     int A,B,C,i,j,k,T,t,now_t,leap;
15     scanf("%d",&T);
16     while(T--)
17     {
18         memset(map,0,sizeof(map));
19         memset(pro,0,sizeof(pro));
20         scanf("%d %d %d %d",&A,&B,&C,&t);
21 
22         for(i = 0;i < A;i++)
23             for(j = 0;j < B;j++)
24                 for(k = 0;k < C;k++)
25                 scanf("%d",&map[i][j][k]);
26 
27         f = r = 0;
28         q[r].x = q[r].y = q[r].z = q[r].t = 0;
29         r++;
30         leap = 1;
31         now_t = 0;
32         pro[0][0][0] = 1;
33         while(f != r)
34         {
35             struct node v;
36             v = q[f++];
37             now_t = v.t;
38             pro[v.x][v.y][v.z] = 1;
39             int a,b,c;
40             for(i = 0;i < 6;i++)
41             {
42                 a = v.x+to[i][0];
43                 b = v.y+to[i][1];
44                 c = v.z+to[i][2];
45                 if(!pro[a][b][c] && !map[a][b][c])
46                 {
47                     if(a>=0&&a<A&&b>=0&&b<B&&c>=0&&c<C)
48                     {
49                         q[r].t = now_t+1;
50                         q[r].x = a,q[r].y = b,q[r].z = c;
51                         if(a == A-1&&b == B-1&&c == C-1)
52                         {
53                             leap = 0;
54                             break;
55                         }
56                         pro[a][b][c] = 1;
57                         r++;
58                     }
59                 }
60             }
61             if(!leap)
62             break;
63         }
64         if(!leap && q[r].t < t)
65         printf("%d\n",q[r].t);
66         else
67         printf("-1\n");
68     }
69     return 0;
70 }

 

posted @ 2012-08-01 20:27  某某。  阅读(121)  评论(0编辑  收藏  举报