Catch that Cow
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 61826 |
|
Accepted: 19329 |
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.
Source
Usaco Open Silver
[Submit] [Go back] [Status] [Discuss]
Home Page Go back to top
#include <stdio.h>#include<string.h>#include<queue>#include<iostream>#include<algorithm>using namespacestd;vis[200005];structnode{intx; intDis;}; Node u,v;intBFsintStartintend) {u.x=start; U.dis=0; Queue<node>Q; Vis[start]=true; Q.push (U); while(!Q.empty ()) {u=Q.front (); Q.pop (); if(u.x==end)returnU.dis; for(intI=0;i<3; i++){ if(i==0) v.x=u.x+1; Else if(i==1) v.x=u.x-1; Else if(i==2) v.x=u.x*2; if(!vis[v.x]&&v.x>=0&&v.x<=100001) {vis[v.x]=true; V.dis=u.dis+1; Q.push (v); } } }}intMain () {intStart,end; while(SCANF ("%d%d", &start,&end)! =EOF) {memset (Vis,false,sizeof(VIS)); intstep=BFS (start,end); printf ("%d\n", step); } return 0;}
POJ 3278 catch that cow BFS (base water)