Loading...

搜索小练#3

题目

// catch that cow
#include <iostream>
using namespace std;

const int N = 1e7+10;

int n, k, x;
int q[N], v[N], dis[N];

int main () {
	cin >> n >> k;
	if (n > k) {cout << n-k; return 0;}
	int s = 0, t = 1;
	q[1] = n, v[n] = 1;
	while (s <= t) {
		++ s;
		for (int i = 1; i <= 3; ++ i) {
			int x = q[s];
			if (i == 1) ++ x; 
			if (i == 2) -- x;
			if (i == 3) x<<=1;
			if (x >= 0 && x <= 100000) 
				if (!v[x]) {
					v[x] = 1;
					q[++ t] = x;
					dis[x] = dis[q[s]] + 1;
				}
		}
	}
	cout << dis[k];
	return 0;
}

PS:

1.before today I break for two days;

2.It is a very easy bfs.

The only season why I cann't solve this problem is I was very sily

posted @ 2020-07-23 14:06  Youngore  阅读(64)  评论(0)    收藏  举报