POJ 3278 Catch that Cow (BFS) __ Search

Source: Internet
Author: User
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


the

The farmer needs to find his cattle, the farmer is now in the position of N, the cow at K Point, assuming the cow is not moving, the farmer has three kinds of walk: step forward (n+1) step Backward (N-1) jump to the current position twice times (2*n)

Ask, how many times to walk to find his cattle.


train of Thought

Simple BFS, the farmer's current position into the queue, and then out of the team to judge each point, and simulate the next move, Note that the need to mark the point of passing, or will burst stack.


AC Code

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <

queue> #include <math.h> #define MAXX 100005 using namespace std;
BOOL Isvisted[maxx];
    struct node {int n;
    int time;
        Node (int n,int time) {this->n=n;
    this->time=time;
}
};
    void BFs (int n,int k) {memset (isvisted,false,sizeof (isvisted));
    Node Ans=node (n,0);
    Isvisted[n]=true;
    queue<node>sk;
    Sk.push (ANS);
        while (!sk.empty ()) {node P=sk.front ();
        Sk.pop ();     Isvisted[p.n]=true;
            Mark traversed by the dot if (p.n==k)///find {ans=p;
        Break
        } if (p.n+1<maxx&&!isvisted[p.n+1])//Three kinds of Sk.push (node (p.n+1,p.time+1));
        if (p.n-1>=0&&!isvisted[p.n-1]) Sk.push (node (p.n-1,p.time+1));
    if (p.n*2<maxx&&!isvisted[p.n*2]) Sk.push (node (p.n*2,p.time+1)); } priNTF ("%d\n", ans.time);
    int main () {int n,k;
    while (~SCANF ("%d%d", &n,&k)) BFS (n,k);
return 0; }

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.