Poj 3278 catch that cow [BFS + queue]

Source: Internet
Author: User

Original question link: http://poj.org/problem? Id = 3278

Algorithm: BFS + queue + STL (C ++)

PS: getting started with BFs. I'm so glad to use BFs.

Catch that cow
Time limit:2000 ms   Memory limit:65536 K
Total submissions:32679   Accepted:10060

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

Question:

FJ wants to catch cows.

Start to input N (FJ position) K (COW position ).

FJ has three ways to move: 1. Take a step forward and take one minute.

2. Take a step backward, which takes one minute.

3. It takes one minute to move forward to the current position by two times N * 2.

Ask FJ the minimum time for catching a cow. PS: the cows won't change.

 

Idea: 1, if FJ is not behind the cow, then he only step back to move to the cow position, that is, N> = K, the output of the N-K can be.

2. Otherwise, BFs + queue search (For details, refer to the Analysis & code area below)

Related algorithms:

1. queue in STL. (PS: Thursday, I learned the true thoughts of BFS on the data structure. Sorry !)

Required header file: STL is # include <iostream> in C ++.

Using namespace STD;

Header file of the queue container # include <queue>

Queue queue usage: FIFO)

Push () // insert element

Pop () // deletes an element

Front () // read the first element of the team

Back () // read the team End Element

Empty () // determines whether the queue is empty

Size () // read the number of current elements in the queue

2. BFS idea: the sequence in which the node performs a breadth-first search.

Search implementation method (non-recursion ):

Algorithm idea: 1. Set a queue Q, start from the vertex, traverse the vertex and let it into the queue;

2. Leave a vertex element and find all adjacent points of the vertex (corresponding to the three methods of Fj ),

Traverse neighboring contacts that have not been traversed and queue them;

3. If the team is empty, continue step 1.

BFS data structure thought detailed introduction: http://zh.wikipedia.org/wiki/BFS

After reading the code of Niu Ren's blog, I finally learned about BFS and orz.

Code 1: C ++ STL & BFS version:

PS: It's strange that the poj has ac. Today, I handed it to HDU To Go To wa. Then I asked kb to know that the queue was not cleared, and poj was input in a single group. This problem does not exist in array simulation because every BFS array subscript call starts from 0.

In addition, Q. Clear (); is not used to clear the queue.
While (! Q. Empty () Q. Pop (); // note that you must clear

 
// Accepted984k79msc ++ 1128b2012-11-10 00:44:26 # include <iostream> # include <queue> # include <cstring> # include <cstdio> using namespace STD; const int maxn = 100001; bool vis [maxn]; // mark the array int step [maxn]; // record the number of steps taken at each position queue <int> q; // define the queue int BFS (int n, int K) {int head, next; q. push (n); // start FJ at N position, n join step [N] = 0; vis [N] = true; // mark accessed while (! Q. empty () // when the queue is not empty {head = Q. front (); // gets the first Q. pop (); // pop up the first pair of for (INT I = 0; I <3; I ++) // three walk-through methods of Fj {if (I = 0) next = head-1; else if (I = 1) Next = head + 1; else next = head * 2; If (next <0 | next> = maxn) continue; // exclude external conditions if (! Vis [next]) // if the next location is not accessed {q. push (next); // step [next] = step [head] + 1; // number of steps + 1 vis [next] = true; // mark accessed} If (next = k) return step [next]; // when the result is traversed, return steps }}int main () {int N, k; while (CIN> N> K) {memset (step, 0, sizeof (STEP); memset (VIS, false, sizeof (VIS); While (! Q. empty () Q. pop (); // note that you need to clear if (n> = k) printf ("% d \ n", n-k) before calling ); else printf ("% d \ n", BFs (n, k);} return 0 ;}

Code 2: C language + BFS + simulated queue versions both HDU and poj can be AC

// Accepted736k0msc ++ 1017b2012-11-10 04:24:09 # include <stdio. h> # include <string. h> const int maxn = 100001; bool vis [maxn]; int N, K; struct node {int X, step ;}; node Q [maxn]; int BFS () {int I; node now, next; int head, tail; head = tail = 0; Q [tail]. X = N; Q [tail]. step = 0; tail ++; vis [N] = true; while (Head <tail) {now = Q [head]; // head ++; // The first for (I = 0; I <3; I ++) {if (I = 0) Next is displayed. X = now. x-1; else if (I = 1) Next. X = now. X + 1; els E next. x = 2 * now. X; If (next. x <0 | next. x> = maxn) continue; // pruning and cross-border exclusion if (! Vis [next. x]) {vis [next. x] = true; next. step = now. step + 1; Q [tail]. X = next. x; Q [tail]. step = next. step; tail ++; If (next. X = k) return next. step ;}}} int main () {While (scanf ("% d", & N, & K )! = EOF) {memset (VIS, false, sizeof (VIS); If (n> = k) printf ("% d \ n", n-k ); else printf ("% d \ n", BFs ();} 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.