HDU2717-Catch That Cow (BFS入门)

题目传送门: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  } 
时间: 2024-11-05 00:54:20

HDU2717-Catch That Cow (BFS入门)的相关文章

HDU2717 Catch That Cow ( BFS )

Catch That Cow Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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

POJ3278 HDU2717 Catch That Cow

问题链接:POJ3278 HDU2717 Catch That Cow. 题意简述:一条线上,人的FJ的起点为K位置,牛在N位置(牛不动),输入正整数K和N.若FJ在x位置,FJ有三种走法,分别是走到x-1.x+1或2x位置.求从K走到N的最少步数. 问题分析:典型的BFS问题.在BFS搜索过程中,走过的点就不必再走了,因为这次再走下去不可能比上次的步数少. 程序中,使用了一个队列来存放中间节点. 需要说明的是,除了BFS方法,这个题应该可以用分支限界法来解,需要更高的技巧. AC的C++语言程

[ACM] hdu 2717 Catch That Cow (BFS)

Catch That Cow 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

HDU 2717 Catch That Cow(bfs)

Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10575    Accepted Submission(s): 3303 Problem Description Farmer John has been informed of the location of a fugitive cow and want

POJ 3278 Catch That Cow(BFS,板子题)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 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,00

POJ 3275 Catch That Cow(bfs)

Catch That Cow 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

Catch That Cow BFS求线性双向最短路径

Catch That Cow 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

poj 3278 Catch That Cow (bfs搜索)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 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,00

HDU2717 Catch That Cow 【广搜】

Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7147    Accepted Submission(s): 2254 Problem Description Farmer John has been informed of the location of a fugitive cow and wants

poj 3278 catch that cow BFS(基础水)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 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,00