Catch that Cow time limit:4000/2000ms (java/other) Memory limit:131072/65536k (Java/other) total submission (s): 56 Accepted Submission (s): 20
problem Description
Farmer John had 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 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?
InputLine
1:two space-separated integers:N and K
OutputLine
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
Source
PKUSearch
Ideas:
This problem is a one-dimensional coordinates to give the starting point and end point, and then let you use two (or can be said to be three) different ways to move, see the minimum amount of time is how much (also can be said to be the smallest number of steps)! Note that walking can move around, but teleport (that is, another way to move forward only), and then you use these three ways to the one-dimensional coordinates of the points in a wide search, the minimum number of steps to reach the end!
The problem is different from the previous one is that the previous is in the two-dimensional plane on the wide search (there are different paths), find the shortest path, and this problem although only one path can go, but the topic has given a limit, that is, can have a different way of walking, can be a step through the style of walking, So this increases the number of paths, so this problem is actually let the shortest path!
Generally we do the shortest path to the problem, we use the method of wide search!
The specific code is as follows:
#include <stdio.h> #include <string.h> #include <algorithm> #include <queue>using namespace std ; #define INF 0xfffffffint n,m,ans;int vis[100005];struct Node {int X;int time;friend bool operator < (node A,node b) {RE Turn A.time>b.time;}} A,temp;int judge () {if (temp.x<0| | temp.x>100000) return 0;if (vis[temp.x]==1) return 0;if (Temp.time>=ans) return 0;return 1;} void BFs () {a.x=n;//assigns the value of the starting point to struct A and then puts the struct A in the pair column to find the points around it (both front and back and twice times the three points) a.time=0;//the first time is 0! priority_queue<node>q;//set a priority queue to put the shortest time to the top Q.push (a),//The array A is pressed into the queue memset (vis,0,sizeof (VIS));//empty tag array vis[n ]=1;//start Mark while (!q.empty ())//If the queue is not empty, do the following to find the end {a=q.top ();//Because the top element is used below, so only if the queue is not empty, the following actions make sense! Q.pop ();//After assigning a value to the queue, because after you have looked around it after the number, it is useless, out of the queue for (int i=0;i<3;i++)//Three different ways to find the end {if (i==0) temp.x=a.x+1;if (i== 1) temp.x=a.x-1;if (i==2) temp.x=a.x*2;temp.time=a.time+1;//each step, the corresponding time to add an if (judge ())//To determine whether this point meets the requirements, if met, then determine whether to find the point {if (temp.x==m)//If the point you are looking for will be the time to reach this point output {ans=temp.time;return;} If it's not the final point, you need to mark the point andPut in the right column, find it around there is no end, has been vis[temp.x]=1;//cycle, know to find the end! Q.push (temp);}} }} int main () {while (scanf ("%d%d", &n,&m)!=eof) {ans=inf; if (n==m)//Because the starting point and end point coincide, the starting point is marked, and (in BFS) The endpoint requirement cannot be marked, so {//so before entering the BFS to determine whether the start and end coincide! printf ("0\n"); Continue }BFS ();//Call Dfs to find the shortest time (that is, the shortest path)! printf ("%d\n", ans);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 3278 Catch that Cow