码迷,mamicode.com
首页 > 其他好文 > 详细

HDU2717-Catch That Cow (BFS入门)

时间:2017-05-24 00:53:36      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:wal   include   retrieve   while   teleport   art   dia   enter   size   

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717

Catch That Cow

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14880    Accepted Submission(s): 4495


Problem Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.

* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.

If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
 

 

Input
Line 1: Two space-separated integers: N and K
 

 

Output
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
 

 

Sample Input
5 17
 

 

Sample Output
4
Hint
The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
 
题意:
从a走到b,你可以向前走一步,向后走一步或者走a*2步,求最少多少步
 
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<queue>
 4 using namespace std;
 5 int v[100010],t[100010];
 6 int main()
 7 {
 8     int n,a,b,i,f;
 9     while(scanf("%d%d",&a,&b)!=EOF)
10     {
11         memset(t,0,sizeof(t));
12         memset(v,0,sizeof(v));
13         queue<int > q;
14         q.push(a);
15         v[a]=1;
16         if(a==b)
17         {
18             printf("0\n");
19             continue;
20         }
21         while(!q.empty())
22         {
23             int ll=q.front(),r=ll-1,l=ll+1,z=ll*2;
24             q.pop();
25             //printf("ll=%d\n",ll);
26             if(z>=0&&z<=100000&&!v[z])
27             {
28                 q.push(z);
29                 v[z]=v[ll]+1;
30             //    printf("v[z]=%d z=%d\n",v[z],z);
31             }
32             if(l>=0&&l<=100000&&!v[l])
33             {
34                 q.push(l);
35                 v[l]=v[ll]+1;
36                 //    printf("v[l]=%d l=%d\n",v[l],l);
37             }
38             if(r>=0&&r<=100000&&!v[r])
39             {
40                 q.push(r);
41                 v[r]=v[ll]+1;
42                 //    printf("v[r]=%d r=%d\n",v[r],r);
43              }
44              
45              if(l==b||r==b||z==b)
46              {
47              
48                  break;
49              }
50                   
51         }
52         
53         printf("%d\n",v[b]-1);
54         
55     }
56     return 0;
57     
58  } 

 

HDU2717-Catch That Cow (BFS入门)

标签:wal   include   retrieve   while   teleport   art   dia   enter   size   

原文地址:http://www.cnblogs.com/ljmzzyk/p/6896780.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!