题解
2026-08-08 19:48:26
发布于:上海
1阅读
0回复
0点赞
从A654(catch that cow)的题解改的:
#include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin >> t;
while(t--){
int n , k;
cin >> n >> k;
queue<int> q;
q.push(n);
int vis[100005] = {0};
int d[100005] = {0};
while(!q.empty()){
int t = q.front();
q.pop();
if(t == k){
cout << d[t] << endl;
}
if(t + 1 <= 100000 && vis[t + 1] != 1){
vis[t + 1] = 1;
d[t + 1] = d[t] + 1;
q.push(t + 1);
}
if(t - 1 >= 0 && vis[t - 1] != 1){
vis[t - 1] = 1;
d[t - 1] = d[t] + 1;
q.push(t - 1);
}
if(t * 2 <= 100000 && vis[t * 2] != 1){
vis[t * 2] = 1;
d[t * 2] = d[t] + 1;
q.push(t * 2);
}
}
}
return 0;
}
这里空空如也





有帮助,赞一个