Catch that cow
Time limit:2000 ms |
|
Memory limit:65536 K |
Total submissions:48127 |
|
Accepted:15077 |
Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a pointN(0 ≤N≤ 100,000) on a number line and the cow is at a pointK(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 pointXTo the pointsX-1 orX+ 1 in a single minute
* Teleporting: FJ can move from any pointXTo the point 2 ×XIn 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 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. algorithm analysis: a BFS search question. At the beginning, I thought it was necessary to find a Unified Algorithm formula to calculate the minimum number of steps from "Start number" to "end number, however, after thinking about it for half a day, I finally thought about solving the problem with violence. But isn't this an algorithm question for BFS? Yes, it is not a common Linear brute force search, but the BFS concept is needed. After careful analysis, you can know that computer programs cannot dynamically decide how to continue from the current step to achieve the best results! (Only in this question) Our idea is: Starting from the "current node", we can go to the remaining three nodes, and the three nodes can reach three nodes respectively, in addition, there are nine vertices. Of course, there may be repeated vertices in these nine points. That is to say, if the point has already been accessed, there will be no need for further access. Therefore, you need to use the number of Mark groups. In this case, you can determine whether each reachable point is the end point. If this point is the end point, return the number of steps to reach the "current node" (recorded using an array ).
# Include <iostream> # include <stdio. h> # include <string. h> # include <queue> # include <algorithm> using namespace STD; int vis [101000], DIS [101000]; int BFS (int n, int K) {queue <int> q; // resume queue Q. push (n); // enter the starting point into the queue vis [N] = 1; // mark the starting point to be accessed by DIS [N] = 0; // The number of steps from the start point to the current node is 0 int curpos; // while (! Q. empty () // judge that the queue is not empty {curpos = Q. front (); // retrieve the first element Q. pop (); If (curpos = k) // if it is equal to the end {return dis [curpos]; // return steps} else {If (curpos-1> = 0 & curpos <= 100000 & Vis [curpos-1] = 0) // determine whether this point is feasible {q. push (curpos-1); // If the row enters the queue standby vis [curpos-1] = 1; // mark the point to be accessed, do not be repeatedly accessed in the Future DIS [curpos-1] = dis [curpos] + 1; // number of steps arriving at this point = number of steps at the current point + 1} If (curpos + 1> = 0 & curpos + 1 <= 100000 & Vis [curpos + 1] = 0) // analogy {q. push (curpos + 1); vi S [curpos + 1] = 1; DIS [curpos + 1] = dis [curpos] + 1 ;} if (curpos * 2> = 0 & curpos * 2 <= 100000 & Vis [curpos * 2] = 0) // analogy {q. push (curpos * 2); vis [curpos * 2] = 1; DIS [curpos * 2] = dis [curpos] + 1 ;}} return 0 ;} int main () {int N, K; while (scanf ("% d", & N, & K )! = EOF) {memset (VIS, 0, sizeof (VIS); memset (DIS, 0, sizeof (DIS); printf ("% d \ n ", BFS (n, k);} return 0 ;}
Poj 3278 catch that cow