POJ-Catch That Cow

No matter how high the mountain, climb, always climb;
the road may be long, go on, will be able to reach.

POJ-Catch That Cow

Title Description

Farmer John has 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 is at a point K (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 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 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?

Entry

Line 1: Two space-separated integers: N and K

Export

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.

Subject to the effect

You are in a certain point, another point in the cow, you can jump directly to their own 2 * n coordinates, you can also walk on their own coordinate n + 1 and n-1 coordinates, ask, you may be able to find the minimum number of steps cattle, cattle position does not move, this is one-dimensional axes.

Analytical title
Each step has three options, but they do not need to go in one direction for a long time, just to solve the current, so there with the idea of ​​a bfs.
Ideas analysis
In fact, the idea is simple, but there is a pit, that restriction.
You can not go to 0 after first, and then you can move forward infinite, but continue to go there a lot of strategy does not make sense. So you want to delete these unnecessary troubles.
0 not come after this solved, but the face of infinite range not know how to draw boundaries.
But we can think of, must not be 100,000, because there may be to go back a little after doubling went to the target point, such a solution is optimal. However, the boundaries of greater than 200,000, then it is better to go directly Stepping back later, so the limit is set to 200,000
the OK idea you can write the code
to note that, to prevent pointer drift, we must first determine the restrictions, then before proceeding.
Secondly, judge whether through the place as much as possible with the array, to open a larger, meet the requirements, but do not use map storage, you can store a hash, map, then popular black tree query hand up not as good as the direct use of arrays and hashes reality.
Secondly
G ++ may be used in q.push (node ​​{1,2}) in this way directly into the queue

Time to AC

#include<algorithm>
#include<iostream>
#include<string.h>
#include<utility>
#include<stdio.h>
#include<vector>
#include<string>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#pragma warning(disable:4244)
#define PI 3.1415926536
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const char char_inf = 127;
ll GCD(ll a, ll b) { return a ? GCD(b, a % b) : a; }
inline ll read() {
	ll c = getchar(), Nig = 1, x = 0;
	while (!isdigit(c) && c != '-')c = getchar();
	if (c == '-')Nig = -1, c = getchar();
	while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
	return Nig * x;
}
inline void out(ll a)
{
	if (a < 0)putchar('-'), a = -a;
	if (a >= 10)out(a / 10);
	putchar(a % 10 + '0');
}
ll qpow(ll x, ll n, ll mod) {
	ll res = 1;
	while (n > 0) {
		if (n & 1)res = (res * x) % mod;
		x = (x * x) % mod; n >>= 1;
	}
	return res;
}
#define read read() 
bool judge[200007];
struct node {
	int x, step;
};
queue<node>q;
node temp;
void bfs(int End) {
	while (!q.empty()) {
		temp = q.front();
		q.pop();
		if (temp.x == End) {
			cout << temp.step << endl;
			return;
		}
		if ((temp.x * 2 <= 200000) && (!judge[temp.x * 2])) {////先判断条件
			judge[temp.x * 2] = true;
			q.push(node{ temp.x * 2,temp.step + 1 });
		}
		if ((temp.x + 1 <= 200000) && (!judge[temp.x + 1])) {////先判断条件
			judge[temp.x + 1] = true;
			q.push(node{ temp.x + 1,temp.step + 1 });
		}
		if ((temp.x >= 1) && (!judge[temp.x - 1])) {//先判断条件
			judge[temp.x - 1] = true;
			q.push(node{ temp.x - 1,temp.step + 1 });
		}
	}
}
int main() {
	int Sta = read, End = read;
	if (Sta >= End) {//因为反向走的方法只有一种,直接出结果就可以了
		cout << Sta - End << endl;
		return 0;
	}
	q.push(node{ Sta,0 });
	judge[Sta] = true;
	bfs(End);
}

By- wheel month

Published 31 original articles · won praise 4 · Views 1160

Guess you like

Origin blog.csdn.net/qq_35339563/article/details/105066221