Poj 3278: Catch that cow (simple one-dimensional wide search)

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

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

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.

Source

Usaco 2007 Open silver Simple one-dimensional wide search. Question: You are at the N position of a one-dimensional coordinate, and the ox is at the K position. You need to capture the ox from N to K. You can have three steps: n + 1, n-1, or N * 2. Please catch the shortest step of the cow. Ideas: The idea of simple and extensive search. There are three jump methods for status redirection. Place the new status in the queue and extract the most advanced status in the queue until the K position is found. Code:
1 # include <iostream> 2 # include <stdio. h> 3 # include <string. h> 4 # include <queue> 5 using namespace STD; 6 7 bool ISW [100010]; 8 9 struct node {10 int X; 11 int s; 12 }; 13 14 bool judge (int x) 15 {16 if (x <0 | x> 100000) 17 return true; 18 if (ISW [x]) 19 Return true; 20 return false; 21} 22 23 int BFS (int sta, int end) 24 {25 queue <node> q; 26 node cur, next; 27 cur. X = sta; 28 cur. S = 0; 29 ISW [cur. x] = true; 30 Q. Push (cur); 31 While (! Q. empty () {32 cur = Q. front (); 33 Q. pop (); 34 if (cur. X = END) 35 return cur. s; 36 // start with 37 int NX; 38 Nx = cur. X + 1; 39 if (! Judge (nx) {40 next. X = NX; 41 next. S = cur. S + 1; 42 ISW [next. x] = true; 43 Q. push (next); 44} 45 Nx = cur. x-1; 46 If (! Judge (nx) {47 next. X = NX; 48 next. S = cur. S + 1; 49 ISW [next. x] = true; 50 Q. push (next); 51} 52 // jump forward 53 Nx = cur. x * 2; 54 if (! Judge (nx) {55 next. X = NX; 56 next. S = cur. S + 1; 57 ISW [next. x] = true; 58 Q. push (next); 59} 60} 61 Return 0; 62} 63 64 65 int main () 66 {67 int N, K; 68 while (scanf ("% d", & N, & K )! = EOF) {69 memset (ISW, 0, sizeof (ISW); 70 int step = BFS (n, k); 71 printf ("% d \ n ", step); 72} 73 return 0; 74}

 

Freecode: www.cnblogs.com/yym2013

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.