Poj3278 getting started with BFS and getting started with poj3278bfs

Source: Internet
Author: User

Poj3278 getting started with BFS and getting started with poj3278bfs
M-Search

Crawling in process... Crawling failed Time Limit:2000 MSMemory Limit:65536KB64bit IO Format:% I64d & % I64u

Submit Status Practice POJ 3278

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. the farmer chased the lost ox. The question gave him the place of the ox, represented by numbers N and K. If the ox did not move, he asked the farmer the minimum number of steps to move to the ox, there are three options for a farmer to move each time: 1 for the position, 1 for the position, and 2 for the position. train of Thought Analysis: Find the minimum number of steps and use BFS to get over the water. However, due to the weak BFS getting started, there are still a lot of problems in the question. BFS generally uses queues for implementation, at the beginning, we used the queue <int> queue, but there was a problem in saving the number of steps. At this time, I chose queue <pair <int, int> to record the position and number of steps. Each time n + 1, n-1, n * 2 are pushed to the queue until n = m. The program can run, and the output in the sample is correct, but the MLE is submitted, which makes me understand the importance of pruning. Finally, the program is trimmed in two aspects, one is to create a tag array and mark every point searched to avoid repeated searches. The second is to use n> m without any doubt. After applying these two pruning policies, A is successfully dropped. Code: # include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
# Include <queue>
# Include <stack>
Using namespace std;
Const int maxn = 4e5 + 5;
Int n, m;
Queue <pair <int, int> q;
Bool hash [maxn];
Int main ()
{
Int m, n;
While (cin> n> m)
{
Memset (hash, false, sizeof (hash ));
Pair <int, int> p;
P. first = n;
P. second = 0;
Hash [n] = true;
Q. push (p );
While (! Q. empty ())
{
Pair <int, int> a, B;
A = q. front ();
If (a. first = m)
{
Cout <a. second <endl;
Break;
}
A. second ++;
B =;
If (B. first> m)
{
B. first = a. first-1;
If (hash [B. first] = false)
{
Hash [B. first] = true; // mark the vertex to indicate that it has passed
Q. push (B );
}
}
If (B. first <m)
{
B. first = a. first + 1;
If (hash [B. first] = false)
{
Hash [B. first] = true; // mark the vertex to indicate that it has passed
Q. push (B );
}
B. first = a. first * 2;
If (hash [B. first] = false)
{
Hash [B. first] = true;
Q. push (B );
}
B. first = a. first-1;
If (hash [B. first] = false)
{
Hash [B. first] = true;
Q. push (B );
}
}
Q. pop (); // the first element of the queue is displayed.
}
While (! Q. empty ())
Q. pop (); // clear the queue
}
Return 0;
} Dream starts! Come on!

Related Article

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.