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 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.

 

Source

USACO 2007 Open Silver

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 using namespace std;
 6 long long l,k;
 7 const int maxn=100000;
 8 bool v[100010];
 9 struct  node
10 {
11     long long x;
12     int step;
13 };
14 bool check(long long a)
15 {
16     if(a<0||a>maxn||v[a]) return false;
17     return true;
18 }
19 int bfs()
20 {
21     node n,m;
22     n.x=l;
23     n.step=0;
24     queue<node> q;
25     q.push(n);
26     v[n.x]=1;
27     while(!q.empty()){
28         n=q.front();
29         q.pop();
30         if(n.x==k) return n.step;
31         //-1
32         m.x=n.x-1;
33         if(check(m.x)){
34             m.step=n.step+1;
35             v[m.x]=1;
36             q.push(m);
37         }
38         //+1
39         m.x=n.x+1;
40         if(check(m.x)){
41             m.step=n.step+1;
42             v[m.x]=1;
43             q.push(m);
44         }
45         //*2
46         m.x=n.x*2;
47         if(check(m.x)){
48             m.step=n.step+1;
49             v[m.x]=1;
50             q.push(m);
51         }
52     }
53     return -1;
54 }
55 int main()
56 {
57     while(cin>>l>>k){
58         memset(v,0,sizeof(v));
59         cout<<bfs()<<endl;
60     }
61     return 0;
62 } 
时间: 2024-12-05 16:06:37

HDU 2717 Catch That Cow(bfs)的相关文章

[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)

题目链接: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): 12615    Accepted Submission(s): 3902 Problem Description Farmer John has been

bfs hdu 2717 Catch That Cow

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

hdu 2717 Catch That Cow(广搜bfs)

题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7909    Accepted Submission(s): 2498 Problem Description Farmer John has been inform

hdu 2717 Catch That Cow

---恢复内容开始--- Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14433    Accepted Submission(s): 4396 Problem Description Farmer John has been informed of the location of a fugitive

杭电 HDU 2717 Catch That Cow

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

hdoj 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): 9276    Accepted Submission(s): 2907 Problem Description Farmer John has been informed of the location of a fugitive cow and wants

hdoj 2717 Catch That Cow 【bfs】

Catch That Cow Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 88   Accepted Submission(s) : 36 Problem Description Farmer John has been informed of the location of a fugitive cow and wants to c

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