POJ 3278 Catch that Cow wide search

Source: Internet
Author: User

HDU 2717 Catch that Cow, topic link

Time limit:5000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 11466 Accepted Submission (s): 3551

Problem 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 inch a single minute
    • TELEPORTING:FJ can move from any point X to the point 2xX 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: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-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

Recommend

Teddy | We have carefully selected several similar problems for you:2102 1372 1240 1072 1180

Test instructions: give you two positions K and N to find the minimum number of steps from K to N.
There are three ways of changing from K: k+1,k-1,k*2.

Queue wide search, a vis array to see if it has been accessed, a step array to record the number of steps.

#include <stdio.h>#include <queue>#include <algorithm>#include <string>#include <iostream>#include <string.h>#include <math.h>#define MAXN 100005using namespace STD;intVIS[MAXN];//Whether the tag has been accessed;intSTEP[MAXN];//Record the number of steps; Queue <int >Q;intSerch (intKintN) {intflag=0, a,mark=0;memset(Vis,0,sizeof(VIS));//array clear 0;    memset(Step,0,sizeof(step));//array clear 0;Q.push (k);//Put k in the queue;vis[k]=1;//marked as 1;     while(!q.empty ())//When the array is not empty;{A=q.front ();//Assign the first value of the queue to A;Q.pop ();//delete queue first;         for(intI=1; i<=3; i++) {if(i==1) flag=a+1;Else if(i==2) flag=a-1;Elseflag=a*2;if(flag>100000|| flag<0)//cross-border judgment;                Continue;if(!vis[flag])//If marked as 0;{step[flag]=step[a]+1;//The current number of steps equals the last step of +1;vis[flag]=1;//marked as 1;Q.push (flag);//queue;                if(flag==n)//If the current value is n;                    returnStep[flag];//Returns the number of steps;}        }    }return 0;//If you are submitting C + +, do not return a value;    //But if it is g++, a return 0 must be required;    //This is a compiler problem, the specific is not very clear;}intMain () {intN,k;//k Chase N;     while(~scanf("%d%d", &k,&n))//Here I write K chase N;{if(k>n)//If k>n, can only through the constant reduction of one get, that is, k-n step;            printf("%d\n", k-n);Else            printf("%d\n", Serch (k,n)); }return 0;}

Before about that return 0,wa, good pit, changed the compiler on a.

T^t, just asked the voyage seniors about that C + + submission, g++ submit different questions, results seniors also do not know, I was the first time to encounter this problem, good worry Mulberry ~ ~
But seniors also said have not seen, he said only in this OJ met is double to use%.f good pit oj~~qaq

POJ 3278 Catch that Cow wide search

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: info-contact@alibabacloud.com 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.