Poj 3278 catch that cow (BFS search)

Source: Internet
Author: User
Tags manual writing
Catch that cow
Time limit:2000 ms   Memory limit:65536 K
Total submissions:46715   Accepted:14673

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.
At first, I saw that this question was very similar to a previous question. The question was directly made using brute force and I had been wondering if it would be enough for Division 2, then at the beginning I also want to use violence to do, but some of the later do not take into account, this question is also more than the copy of the http://blog.csdn.net/whjkm/article/details/26985421 of the problem (once that the link ), later, I carefully read the question. There is a big difference between the question and the question. The simple question is that it starts from 0 and cannot be rolled back; there are three situations for this question: 1 forward, 1 backward, and 2 times forward by transmission. At the beginning, I couldn't think of BFS. I saw that the classification of this question was BFs, And then I used BFS as the handler. The basic idea of this topic is the three-entry BFs. First, we will queue the three situations of the first point, mark the point that has been asked, and then join the three cases of the next position, here there are also pruning, which improves the efficiency a lot until you find the final point. The following code simulates a queue using arrays;
# Include <cstdio> # include <cstring> const int maxn = 200030; typedef struct queue // a struct that stores the position of this vertex, and the time {int count, step;} queue; queue [maxn]; // queue array int visit [maxn]; // queue query array void BFS (int n, int K) {int front = 0, rear = 0; queue [rear]. step = N; // enter the initial position in the queue [rear ++]. count = 0; visit [N] = 1; // mark the while (front <rear) {queue q = queue [Front ++]; If (Q. step = k) // locate the destination position {printf ("% d \ n", Q. count); break;} If (Q. step-1> = 0 &&! Visit [q. step-1]) // pay attention to the pruning condition. Be sure to be 0 {visit [q. step] = 1; queue [rear]. step = Q. step-1; // search for a backend queue [rear ++]. count = Q. count + 1; // time + 1} If (Q. step <= K &&! Visit [q. step + 1]) // pruning {visit [q. step + 1] = 1; queue [rear]. step = Q. step + 1; // search for a queue [rear ++]. count = Q. count + 1;} If (Q. step <= K &&! Visit [q. step * 2]) // pruning {visit [q. step * 2] = 1; // transfer the queue [rear]. step = Q. step * 2; queue [rear ++]. count = Q. count + 1 ;}}int main () {int N, K; while (scanf ("% d", & N, & K )! = EOF) {memset (visit, 0, sizeof (visit); BFS (n, k) ;}return 0 ;}

It can also be written with STL, so the efficiency is not as high as manual writing, and bool for marking arrays saves memory than swap; I wrote it again using STL, with the same idea as above;
#include <cstdio>#include <cstring>#include <queue>using namespace std;const int maxn=200030;queue<int>q;int Count[maxn];bool visit[maxn];void bfs(int n,int k){   q.push(n);   visit[n]=1;   Count[n]=0;   while(!q.empty())   {       int x=q.front();       q.pop();       if(x==k)       {           printf("%d\n",Count[x]);           break;       }       if(x-1>=0 && !visit[x-1])       {           q.push(x-1);           visit[x-1]=1;           Count[x-1]=Count[x]+1;       }       if(x<=k && !visit[x+1])       {           q.push(x+1);           visit[x+1]=1;           Count[x+1]=Count[x]+1;       }        if(x<=k && !visit[2*x])       {           q.push(x*2);           visit[x*2]=1;           Count[x*2]=Count[x]+1;       }   }}int main(){    int n,k;    while(scanf("%d%d",&n,&k)!=EOF)    {        memset(visit,0,sizeof(visit));        bfs(n,k);    }    return 0;}

It looks good to see a way of writing to others. You can search in three directions.
# Include <iostream> # include <queue> # define size 100001 using namespace STD; queue <int> X; bool visited [size]; int step [size]; int BFS (int n, int K) {int head, next; // enter the Start Node X. push (n); // mark n already asked visited [N] = true; // The number of start steps is 0 step [N] = 0; // while (! X. empty () {// retrieve head = x. front (); // The frontend X. pop (); // search for (INT I = 0; I <3; I ++) {if (I = 0) Next = head-1; else if (I = 1) Next = head + 1; else next = head * 2; // If (next> size | next <0) Continue is not taken into account in the out-of-bounds mode; // judge if (! Visited [next]) {// node Team X. push (next); // number of steps + 1 step [next] = step [head] + 1; // mark the node that has been submitted and asked visited [next] = true ;} // find exit if (next = k) return step [next] ;}} int main () {int N, K; CIN >>> n >> K; if (n> = k) {cout <n-k <Endl;} else {cout <BFS (n, k) <Endl;} return 0 ;}


Poj 3278 catch that cow (BFS search)

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.