Catch that Cow
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 61317 |
|
Accepted: 19155 |
Description
Farmer John had 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 are at a point K (0≤k≤100,000) on the same number Line. Farmer John has modes of transportation:walking and teleporting.
* WALKING:FJ can move from any point X to the points X-1 or X + 1 inch a single minute
* TELEPORTING:FJ can move from any point X to the point 2xX 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-Farmer John to reach the fugitive cow are to move along the following PATH:5-10-9-18-17, which takes 4 Minutes.
Source
Usaco five Open Silver RE: There are two points on the axis (A, B), the minimum number of steps required to get a close to B, there are three ways to go, (x-1), (x+1), (2*x); a > b→→ output; else:: Bfs;
1#include <queue>2#include <cstdio>3#include <cstring>4#include <iostream>5 using namespacestd;6 7 Const intMAXN =100000+5;8 intVIS[MAXN], CNT[MAXN];9Queue <int>Q;Ten One voidBfs () A { - intNX; - while(!q.empty ()) the { - intx =Q.front (); Q.pop (); - for(inti =0; I <3; i++)//three kinds of walking method; - { + if(i = =0) NX = x +1; - Else if(i = =1) NX = x-1; + ElseNX =2*x; A if(NX >=0&& NX <=100000&&!vis[nx])//Traverse to the last to find the best? :: Pruning? at { -VIS[NX] =1; -CNT[NX] = Cnt[x] +1; - Q.push (NX); - } - } in } - } to + intMain () - { the intm, N; * while(~SCANF ("%d%d", &m, &N)) $ {Panax Notoginseng if(M >N) - { theprintf"%d\n", M-n); + return 0; A } thememset (Vis,0,sizeof(Vis)); +VIS[M] =1; -CNT[M] =0; $ Q.push (m); $ Bfs (); -printf"%d\n", Cnt[n]); - } the return 0; -}
Poj3278--catch that Cow (BFS)