C41/T4 最难题题解-全网首发
2025-02-17 19:34:07
发布于:江苏
56阅读
0回复
0点赞
#include <iostream>
#include <queue>
#include <vector>
#include <utility>
using namespace std;
int bfs(int a, int b) {
vector<bool> visited(10005, false);
queue<pair<int, int>> q;
q.push({a, 0});
visited[a] = true;
while (!q.empty()) {
auto [current, steps] = q.front();
q.pop();
if (current == b) {
return steps;
}
int next1 = current * 2;
if (next1 <= 10000 && !visited[next1]) {
q.push({next1, steps + 1});
visited[next1] = true;
}
int next2 = current - 1;
if (next2 >= 0 && !visited[next2]) {
q.push({next2, steps + 1});
visited[next2] = true;
}
}
return -1;
}
int main() {
int T;
cin >> T;
while (T--) {
int a, b;
cin >> a >> b;
cout << bfs(a, b) << endl;
}
return 0;
}
全部评论 1
AI味略浓
2025-02-19 来自 浙江
0并不是哦
2025-02-19 来自 江苏
0
有帮助,赞一个