Openjudge 2971: Catch the bull &&p1588 the Lost cow

Source: Internet
Author: User

Konjac Konjac's first-time hair

After a thoughtful

Selected a classic BFS (queue) problem

2971: Catch the Bull

Total time limit:
2000ms
Memory Limit:
65536kB
Describe

The farmer knows the position of a bull and wants to catch it. Both the farmer and the ox are located on the axis, the farmer starts at point N (0<=n<=100000) and the ox is at point K (0<=k<=100000). Farmers have two ways of moving:

1. Move from X to X-1 or x+1, one minute per move 2, moving from X to 2*x, each move takes a minute Suppose the cow is not aware of the farmer's actions and stands still. How long does it take at least for the farmer to catch the bull?

Input
two integers, N and K
Output
an integer, the minimum number of minutes a farmer can take to catch a cow.
Sample input
5 17
Sample output
4

The first thing that comes to mind when you see this problem is the search algorithm

This Konjac Konjac personally think the problem BFS can solve problem easily

The general idea is as follows:

Use a long queue to record the distance that can be reached

So that the same distance at the same time goes at the same depth

In the code, the depth of the number of I is dep[i]

So the depth of the first value of K is the answer.

No more talking.

On the Code

#include <iostream>//Plain Header fileusing namespacestd;intn,k,que[100005],dep[100005],head,tail;//standard variable names don't have to explain.BOOLa[100005];//ensure minimum number of stepsintMain () {CIN>>n>>K; if(n>=k) {cout<<n-k;//You can go back only one step at a        return 0; } A[n]=true; head=1, tail=1; dep[1]=0; que[1]=N;  while(head<=tail) {        if(que[head]+1==k| | que[head]-1==k| | que[head]*2==k)//the next operation will make the queue more than three elements, so you have to judge beforehand{cout<<dep[head]+1; return 0;//Perfect end!!!         }        if(!a[que[head]+1]&&que[head]+1<100005)//go to Law 1{Tail++; Que[tail]=que[head]+1; Dep[tail]=dep[head]+1; A[que[tail]]=true; }        if(!a[que[head]-1]&&que[head]-1>=0)//go to Law 2{Tail++; Que[tail]=que[head]-1; Dep[tail]=dep[head]+1; A[que[tail]]=true; }        if(!a[que[head]*2]&&que[head]*2<100005)//go to Law 3{Tail++; Que[tail]=que[head]*2; Dep[tail]=dep[head]+1; A[que[tail]]=true; } head++;//move the head pointer    }     return 0; }

Topic Split Line ~ ~ ~

About this cow.

There's a similar yellow question on my big Rocky valley.

P1588 Lost Cattle topic description

FJ lost his cow, and he decided to recover his cattle. It is known that the FJ and cows are in a straight line with the initial positions x and Y, assuming the cows are not moving.

FJ's way of walking is very special: every time he can go further, step back or walk directly to 2*x's position. He needs at least a few steps to catch up with his cows.

Input output Format input format:

The first behavior is an integer t (≤10), which represents the number of data groups, followed by a two positive integer x and y (0<x,y≤10^5) for each row, representing the coordinates of the FJ and the cattle, respectively.

Output format:

For each set of data, the minimum number of steps is output.

Input and Output sample input sample #: Copy
1 5 17
Output Example # #: Replication
4

This problem just need to write the above code into the function.

The code is as follows

#include <iostream>#include<cstring>//I do not like to use the universal head fileusing namespacestd;intn,k,que[200005],dep[200005],head,tail;BOOLa[200005];//Rokua data is more demanding, array range needs to be changedvoidBFs () {if(n>=k) {cout<<n-k<<endl;//You can go back only one step at a        return; } A[n]=true; head=1, tail=1; dep[1]=0; que[1]=N;  while(head<=tail) {        if(que[head]+1==k| | que[head]-1==k| | que[head]*2==k)//the next operation will make the queue more than three elements, so you have to judge beforehand{cout<<dep[head]+1<<Endl; return; }        if(!a[que[head]+1]&&que[head]+1<100005)//go to Law 1{Tail++; Que[tail]=que[head]+1; Dep[tail]=dep[head]+1; A[que[tail]]=true; }        if(!a[que[head]-1]&&que[head]-1>=0)//go to Law 2{Tail++; Que[tail]=que[head]-1; Dep[tail]=dep[head]+1; A[que[tail]]=true; }        if(!a[que[head]*2]&&que[head]*2<100005)//go to Law 3{Tail++; Que[tail]=que[head]*2; Dep[tail]=dep[head]+1; A[que[tail]]=true; } head++;//move the head pointer    } }intMain () {intS,i; CIN>>s;  for(i=1; i<=s;i++) {cin>>n>>K; memset (que,0,sizeof(que)); memset (DEP,0,sizeof(DEP)); Memset (A,0,sizeof(a));    BFS (); }    return 0;}

Okay, this is the perfect end.

Openjudge 2971: Catch the cow &&p1588 lost cattle

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.