Catch that Cow
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 64176 |
|
Accepted: 20156 |
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 in a single minute
* TELEPORTING:FJ can move from any point x to the point 2x 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:
Nand
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. Test instructions: N can add one, minus one, multiply by two by several changes to K,n; BFS Search again
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <queue>5#include <string.h>6#include <algorithm>7 using namespacestd;8 intn,k,num[100000+5];9 Ten voidBFsinty) One { Aqueue<int>Q; - Q.push (y); - while(Q.size ()) the { - intFX; - intx =Q.front (); - Q.pop (); + if(x = =k) - Break; + for(inti =0; I <3; i++) A { at if(i = =0) -FX = x +1; - Else if(i = =1) -FX = x-1; - Else if(i = =2) -FX = x *2; in if(FX >=0&& FX <=100000&& NUM[FX] >Num[x]) - { toNUM[FX] = Num[x] +1; + Q.push (FX); - } the } * } $ }Panax Notoginseng intMain () - { the while(SCANF ("%d%d", &n,&k)! =EOF) + { A for(inti =0; I <100005; i++) theNum[i] =100000000; +Num[n] =0; - BFS (n); $printf"%d\n", Num[k]); $ } - - return 0; the}
View Code
Poj3278catch that Cow (bfs+ water problem)