Bzoj 1646: [Usaco2007 open]catch that Cow catch that cow (BFS)

Source: Internet
Author: User

BFS ...

--------------------------------------------------------------------------------------------

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <queue>#define REP (i, n) for (int i = 0; i < n; i++)#define CLR (x, C) memset (x, C, sizeof (x))using namespace std;const int MAXN = 200000;const int inf = 0X3F3F3F3F;queue< int > Q;int d[MAXN];int main () {//freopen ("test.in", "R", stdin);int n, K;cin >> n >> K;CLR (d, INF);d[N] = 0;Q.push (n);While (! Q.empty ()) {int x = Q.front ();Q.pop ();if (x = = k)Break ;#define OK (x) (0 <= x && x <= 100000)if (OK (x + 1) && d[x + 1] > d[x] + 1) {d[x + 1] = d[x] + 1;Q.push (x + 1);}if (ok (x-1) && d[x-1] > d[x] + 1) {d[X-1] = d[x] + 1;Q.push (x-1); }if (ok (x << 1) && d[x << 1] > d[x] + 1) {d[x << 1] = d[x] + 1;Q.push (x << 1);}}cout << d[K] << "\ n";return 0;}

--------------------------------------------------------------------------------------------

1646: [Usaco2007 open]catch that Cow catch the cow Time Limit:5 Sec Memory Limit:MB
Submit:764 Solved:361
[Submit] [Status] [Discuss] Description

Farmer John had been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point n (0 <= n <= 100,000) on a number line and the cow are at a point K (0 <= K <= 100,000) on The same number line. Farmer John has modes of transportation:walking and teleporting. * WALKING:FJ can move from any point X to the points X-1 or x+1 in a single minute * TELEPORTING:FJ can move from any PO int X to the point 2*x in 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?

Farmer John was informed that one of his cows had escaped! So he decided to send the cow back soon. They all stood on the axis. John is in N (o≤n≤100000), where the cows are at K (o≤k≤100000). John has two ways to move, walk and teleport: A walk per second allows John to walk from Z to X+l or x-l, while the teleport allows him to disappear from X in 1 seconds and appear at 2x. The escaped cow, however, tragically did not find out how bad his situation was, and was standing there motionless. So, how much time does John need to catch the bull? Input

* Line 1:two space-separated integers:n and K

there are only two integers n and K.

Output

* Line 1:the least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

the shortest time. Sample Input 5 17
Farmer John starts at point 5 and the fugitive cow are at point 17.

Sample Output 4

OUTPUT DETAILS:

The fastest-Farmer John to reach the fugitive cow are to
Move along the following PATH:5-10-9-18-17, which takes 4 minutes.HINT

Source

Silver

Bzoj 1646: [Usaco2007 open]catch that Cow catch that cow (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.