首页 > HDU 2717 Catch That Cow(BFS)

HDU 2717 Catch That Cow(BFS)

题目链接

好裸,BFS。杭电多组。。2A。。

 1 #include 
 2 #include <string.h>
 3 int p[100001],o[100001];
 4 int main()
 5 {
 6     int n,k,i,j,start=0,end=0,num=0;
 7     while(scanf("%d%d",&n,&k)!=EOF)
 8     {
 9         memset(o,0,sizeof(o));
10         start=0,end=0,num=0;
11         p[start] = n;
12         o[n] = 1;
13         for(;;)
14         {
15             if(o[k]) break ;
16             num ++;
17             j = 1;
18             for(i = start; i <= end; i ++)
19             {
20                 if(p[i]+1 <= 100000)
21                 {
22                     if(o[p[i]+1] == 0)
23                     {
24                         p[end+j] = p[i]+1;
25                         o[p[i]+1] = 1;
26                         j ++;
27                     }
28                 }
29                 if(p[i]-1 >= 0)
30                 {
31                     if(o[p[i]-1] == 0)
32                     {
33                         p[end+j] = p[i]-1;
34                         o[p[i]-1] = 1;
35                         j ++;
36                     }
37                 }
38                 if(p[i]*2 <= 100000)
39                 {
40                     if(o[p[i]*2] == 0)
41                     {
42                         p[end+j] = p[i]*2;
43                         o[p[i]*2] = 1;
44                         j ++;
45                     }
46                 }
47             }
48             start = end + 1;
49             end = end + j - 1;
50         }
51         printf("%d
",num);
52     }
53     return 0;
54 }

 

转载于:https://www.cnblogs.com/naix-x/archive/2012/05/30/2526931.html

更多相关: