Catch that Cow
| Time Limit: 2000MS |
|
Memory Limit: 65536K |
| Total submissions: 70246 |
|
accepted: 22095 |
Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her. He starts in a point N (0≤n≤100,000) on a number line and the cow are at point K (0≤k≤100,000) on the same number Line. Farmer John has two modes of transportation:walking and teleporting.
* WALKING:FJ can move points X-1 or X + 1 in a single minute
* TELEPORTING:FJ can move from all point X to the "2xX in a" single minute.
If the cow, unaware of it pursuit, does not move at all, how long is 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 way for farmer John to reach the fugitive cow are to move along the following path:5-10-9-18-17, which TA Kes 4 minutes.
Source Usaco 2007 Open Silver
Search every state.
Remember the special sentence
Accode:
#include <map> #include <queue> #include <cmath> #include <cstdio> #include <cstring> #
Include <stdlib.h> #include <iostream> #include <algorithm> #define MAXN 100100 using namespace std;
int n,k;
struct node{int x;
int t;
};
BOOL VIS[MAXN];
Node My,now,s1,s2,s3; BOOL Judge (Node x) {if (x.x<0| | x.x>100000| |
VIS[X.X]) return false;
return true;
} void BFs () {if (n>=k) {printf ("%d\n", n-k);
Return
} queue<node>q;
memset (Vis) (vis,0,sizeof);
My.x=n;
my.t=0;
Q.push (my);
Vis[n]=1;
while (!q.empty ()) {Now=q.front (); Q.pop ();
S1=now;s1.t++;s1.x+=1;
if (judge (S1)) {if (s1.x==k) {printf ("%d\n", s1.t);
Return
}else {vis[s1.x]=1;
Q.push (S1);
}} s2=now;s2.t++;s2.x-=1; if (judge (S2)) {if (s2.x==k) {printf ("%d\n", s2.t);
Return
}else {vis[s2.x]=1;
Q.push (S2);
}} s3=now;s3.t++;s3.x*=2;
if (judge (S3)) {if (s3.x==k) {printf ("%d\n", s3.t);
Return
}else {vis[s3.x]=1;
Q.push (S3);
int main () {while (~scanf ("%d%d", &n,&k)) BFS ();
return 0;
}