Link:
http://poj.org/problem?id=3278
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 62113 |
|
Accepted: 19441 |
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.
Code:
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<queue>using namespacestd;#defineN 110000structnode{intx, Step;};intS, E;BOOLVis[n];intBFS (ints) {node p, q; P.x= S, P.step =0; memset (Vis,false,sizeof(VIS)); Vis[s]=true; Queue<node>p; Q.push (P); while(Q.size ()) {p=Q.front (), Q.pop (); if(p.x = = e)returnP.step; for(intI=0; i<3; i++) { if(i==0) q.x= p.x +1; Else if(i==1) q.x= p.x-1; Else if(i==2) q.x= p.x *2; Q.step= P.step +1; if(q.x>=0&& q.x<n &&!vis[q.x]) {Q.push (Q); Vis[q.x]=true; } } } return-1;}intMain () { while(SCANF ("%d%d", &s, &e)! =EOF) { intAns =BFS (s); printf ("%d\n", ans); } return 0;}
(Guang Search) Catch that Cow--poj--3278