Poj 3278 Catch That Cow, poj3278

Source: Internet
Author: User

Poj 3278 Catch That Cow, poj3278
Catch That Cow Time Limit: 4000/2000 ms (Java/Other) Memory Limit: 131072/65536 K (Java/Other) Total Submission (s): 56 Accepted Submission (s): 20Problem 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:NAndK  
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
 
Source PKUSearch

 

Ideas:

This question is to give the start and end points in one-dimensional coordinates, and then let you move them in two (or three) different ways, it depends on the minimum time of the phone bill (or the minimum number of steps )! It should be noted that walking can move left and right, but moving instantly (that is, another method can only move forward). Then you can use these three methods to search for vertices in one-dimensional coordinates, how many steps can be used to reach the destination!

The difference between this question and the previous question is that the previous question is to perform a wide search (with different paths) on a two-dimensional plane to find the shortest path, although this question has only one path to go, it imposes another restriction, that is, it can have different ways of walking, and it can take a leap step by step, therefore, the number of paths is increased in this way. Therefore, this question is actually used to find the shortest path!

In general, we solve the problem of finding the shortest path. We will use the extensive search method!

The Code is as follows:

 

# Include <stdio. h> # include <string. h >#include <algorithm> # include <queue> using namespace std; # define INF 0 xfffffffint n, m, ans; int vis [100005]; struct node {int x; int time; friend bool operator <(node a, node B) {return. time> B. time ;}} a, temp; int judge () {if (temp. x <0 | temp. x> 100000) return 0; if (vis [temp. x] = 1) return 0; if (temp. time> = ans) return 0; return 1;} void bfs () {. x = n; // assign the value of the starting point to struct a and place struct a in the Peer column to search for its surroundings. A. time = 0; // the start time is 0! Priority_queue <node> q; // set a priority queue to put the shortest time to q. push (a); // press array a to memset (vis, 0, sizeof (vis) in the queue; // clear the mark array vis [n] = 1; // mark the start point while (! Q. empty () // If the queue is not empty, perform the following operations to find the destination {a = q. top (); // The following operations make sense only when the queue is not empty because the pair-top element is used below! Q. pop (); // After the value is assigned, the queue is output. After you find the number around it, it is useless. Let's get out of the queue for (int I = 0; I <3; I ++) // three different methods to find the endpoint {if (I = 0) temp. x =. x + 1; if (I = 1) temp. x =. x-1; if (I = 2) temp. x =. x * 2; temp. time =. time + 1; // for each step, the corresponding time must be followed by an if (judge () // determines whether the point meets the requirements. if yes, then judge whether it is the point {if (temp. x = m) // if it is the point to be found, the time used to reach this point will be output {ans = temp. time; return;} // if it is not the final vertex, You need to mark the vertex and put it in the right column to find whether there is an end point around it, always vis [temp. x] = 1; // loop, until the end point is found! Q. push (temp) ;}}} int main () {while (scanf ("% d", & n, & m )! = EOF) {ans = INF; if (n = m) // It is marked before the start point (in bfs) because the start point and the end point overlap) the end point cannot be marked, so {// you must determine whether the start point and the end point overlap before entering bfs! Printf ("0 \ n"); continue;} bfs (); // call dfs to find the shortest time (that is, the shortest path )! Printf ("% d \ n", ans);} return 0 ;}


 

 

 

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.