不对,会输出7470304。
MY WA code:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
struct node {
int x;
int step;
};
int t;
bool vis[100001];
int bfs(int l,int r) {
queue<node> q;
while(!q.empty()) q.pop();
q.push(node{l,0});
while(!q.empty()) {
node n1=q.front();
q.pop();
if(n1.x==r) {
return n1.step;
}
if(n1.x-1>=1&&vis[n1.x-1]==0) q.push(node{n1.x-1,n1.step+1}),vis[n1.x-1]=1;
if(n1.x+1<=100001&&vis[n1.x+1]==0) q.push(node{n1.x+1,n1.step+1}),vis[n1.x+1]=1;
if(n1.x*2<=100001&&vis[n1.x*2]==0) q.push(node{n1.x*2,n1.step+1}),vis[n1.x*2]=1;
}
}
int main() {
cin>>t;
while(t--) {
int l,r;
cin>>l>>r;
cout<<bfs(l,r)<<endl;
}
return 0;
}