Untitled

 avatar
unknown
c_cpp
2 years ago
798 B
4
Indexable
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 5;
bool mark[maxn];
vector <int> g[maxn];
int a, b, dis[maxn];
queue <int> q;


void graph(int u) {
	for (int x = u / 2; x <= b * 2; x++) {
//		cout << x << " ";
		int y = x - 1;
//		cout << y << " ";
		int z = x * 2;
		if (y > 0) g[x].push_back(y);
		if (z > 0) g[x].push_back(z);
	}
}

void bfs(int u) {
	q.push(u);
	mark[u] = true;
	dis[u] = 0;
	while (!q.empty()) {
		int v = q.front();
		for (auto w : g[v]) {
			if (!mark[w]) {
//				cout << w << '\n';
				mark[w] = true;
				dis [w] = dis[v] + 1;
				q.push(w);
			}
		}
		q.pop();
	}
}

int main() {
	cin >> a >> b;
	if (b < a) {
		cout << a - b;
	}
	else {
		graph(a);
		bfs(a);
		cout << dis[b] - dis[a];
	}
return 0;
}
Editor is loading...