Poj 3278 Catch That Cow (bfs search), pojbfs

Source: Internet
Author: User

Poj 3278 Catch That Cow (bfs search), pojbfs
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 thought this question was very similar to the previous one. I used brute force to solve the question. I always decided whether I could divide the question by two. Then I also wanted to use brute force at the beginning, but later I do some of the circumstances can not be considered, this question is also more than the copy of the http://blog.csdn.net/whjkm/article/details/26985421 (the previous question of The Link), and then carefully read the question, there is still a big difference with that question. The simple question is that it starts from 0 and cannot be moved back. There are three situations for this question: 1, subtract 1 backward and use 2 times the forward distance of the transmission. At first, I couldn't think of bfs. I saw that the classification of this question was bfs, And then I used bfs for it. The main idea of this topic is the three-entry bfs, which first queues the three situations of the first point, marks that the point has been accessed, and then queues the three cases of the next position, here there is also pruning, which improves the efficiency a lot until the final point is found. 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 queue [maxn]; // Queue array int visit [maxn]; // access the 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 access 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, which must be equal to 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 ;}

You can also use STL to write data, which is less efficient than manually writing data, and bool is used to mark arrays to save memory; 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 accessed visited [n] = true; // The number of start steps is 0 step [n] = 0; // when the queue is not empty 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 that the node has accessed 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 ;}





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.