Catch that cow (breadth-first search _ BFS)

Source: Internet
Author: User
Catch that cow
Time limit:2000 ms   Memory limit:65536 K
Total submissions:48036   Accepted:15057

Description

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a pointN(0 ≤N≤ 100,000) on a number line and the cow is at a pointK(0 ≤K≤ 100,000) on the same number line. Farmer John has two modes of transportation: Walking and teleporting.

* Walking: FJ can move from any pointXTo the pointsX-1 orX+ 1 in a single minute
* Teleporting: FJ can move from any pointXTo the point 2 ×XIn 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
 
Question: enter two numbers N and K. Calculate the minimum number of steps from N to K. You can move forward 1 to backward 1 or the current position * 2;
 
# Include <stdio. h> # include <string. h> # include <stdlib. h ># include <algorithm> using namespace STD; struct node {int X; // int ans at the current position; // number of steps taken} Q [1000010]; int vis [1000010]; // indicates whether the variable is accessed; int JX [] = {-}; // returns 1 or advances 1; struct node T, F; int N, K; void BFS () {int I; int S = 0, E = 0; // the pointer simulates the queue. Add e ++ to the queue and propose several s ++ memset (VIS, 0, sizeof (VIS); T. X = N; // The current initial position vis [T. x] = 1; // marked as 1 indicates that you have accessed it; T. ans = 0; // the initial number of steps is 0; Q [E ++] = T; // Add the current number of steps to the queue while (S <E) // when the queue is not empty {T = Q [s ++]; // propose if (T. X = k) // if the number is exactly equal to the number of direct output steps at the target position {printf ("% d \ n", T. ans); break;} for (I = 0; I <3; I ++) // I = 0 step back, I = 1 step forward, I = 2 position * 2 at this time; {if (I = 2) {f. X = T. x * 2;} else {f. X = T. X + JX [I];} If (F. x> = 0 & F. x <= 100000 &&! Vis [F. x]) {f. ans = T. ans + 1; Q [E ++] = f; vis [F. x] = 1 ;}}} int main () {While (~ Scanf ("% d", & N, & K) {BFS ();} return 0 ;}

Catch that cow (breadth-first search _ BFS)

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.