POJ 3278 Catch that Cow (BFS)

Source: Internet
Author: User

Transmission Door Catch that Cow
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 80273 Accepted: 25290
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 point x to the point 2x 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?

InputLine 1:two space-separated integers: N and K OutputLine 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
Hintthe fastest-Farmer John to reach the fugitive cow are to move along the following path:5-10-9-18-17, which Tak Es 4 minutes.Ideas

Test Instructions: given the starting and ending points, there are three ways to go, assuming that now, then you can choose the next time to reach Now-1 or now + 1 or 2*now, ask the beginning to the end of the minimum number of steps required.

Solving :BFS, the next state is three selectable positions.

#include <iostream> #include <cstdio> #include <cstring>using namespace std;const int maxn = 100005;int Step[maxn],que[maxn],vis[maxn];int BFS (int st,int ed) {int E = 0,f = 0;que[f++] = st;vis[st] = 1;for (;;) {int now = que[e];if (today = ed) return Step[now];if (now + 1 < MAXN &&!vis[now + 1]) Step[now + 1] = Step[now] + 1,vis[now + 1] = 1,que[f++] = now + 1;if (now-1 >= 0 &&!vis[now-1]) step[now-1] = Step[now] + 1,vis[now  -1] = 1,que[f++] = now-1;if (2*now < MAXN &&!vis[2*now]) Step[2*now] = Step[now] + 1,vis[2*now] = 1,que[f++] = 2 * NOW; e++;}} int main () {int N,k;memset (vis,0,sizeof (Vis)), scanf ("%d%d", &n,&k), if (N >= K) printf ("%d\n", n-k); elseprintf ("%d\n", BFS (N,k)); return 0;}

  

POJ 3278 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.