代码见下“
#include<bits/stdc++.h>
using namespace std;
int bfs(int x, int y) {
queue<int> q;
unordered_map<int, int> dist;
q.push(x);
dist[x] = 0;
while (!q.empty()) {
int t = q.front();
q.pop();
if (t == y) return dist[t];
if (t < y && !dist.count(t * 2)) {
q.push(t * 2);
dist[t * 2] = dist[t] + 1;
}
if (t > 0 && !dist.count(t - 1)) {
q.push(t - 1);
dist[t - 1] = dist[t] + 1;
}
}
return -1;
}
int main() {
int T;
cin >> T;
while (T--) {
int x, y;
cin >> x >> y;
cout << bfs(x, y) << endl;
}
return 0;
}