Catch that Cow

Source: Internet
Author: User

Catch that Cowtime limit:4000/2000ms (java/other) Memory limit:131072/65536k (Java/other) total submission (s): 113 Accepted Submission (s): 46Problem 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?

Inputline 1:two space-separated integers: Nand K

Outputline 1:the least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample INPUT5 17

Sample Output4
1#include <stdio.h>2#include <queue>3#include <string.h>4 using namespacestd;5 Const intmaxn=100010;6 intVISIT[MAXN];7 intnow[3];8 intn,k,step,t;9 voidBFs () {Tenqueue<int>DL; One Dl.push (N); Avisit[n]=1; -     if(n!=K) { -      while(!Dl.empty ()) { thet=dl.size (); -step++; -          while(t--){ -now[0]=dl.front () +1; +now[1]=dl.front ()-1; -now[2]=dl.front () *2; + Dl.pop (); A              for(intI=0;i<3; i++){ at                 if(now[i]==k)return; -                 if(now[i]>0&&now[i]<=100000&&!visit[now[i]]) {//drunk, a 0 less, wrong n times; ;;; -visit[now[i]]=1;d L.push (Now[i]); -                 } -             } -         } in     } - } to } + intMain () { -      while(~SCANF ("%d%d",&n,&K)) { thememset (Visit,0,sizeof(visit)); *step=0; $ BFS ();Panax Notoginsengprintf"%d\n", step); -     } the     return 0; +}

Catch that Cow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.