0分求助!全RE
查看原帖
0分求助!全RE
469311
chen_kun楼主2023/12/16 10:58
#include<bits/stdc++.h>
using namespace std;
long long n,m,x,vis[100010],t;
queue<int>q1,q2;
void bfs(){
	q1.push(n);
	q2.push(0);
	while(!q1.empty()){
		x=q1.front();
		if(x==m){cout<<q2.front()<<"\n";return;}
		if(vis[x+1]==0&&x+1<=100000) q1.push(x+1),q2.push(q2.front()+1),vis[x+1]=1;
		if(vis[x-1]==0&&x-1>0) q1.push(x-1),q2.push(q2.front()+1),vis[x-1]=1;
		if(vis[x*2]==0&&x*2<=100000) q1.push(x*2),q2.push(q2.front()+1),vis[x*2]=1;
		q1.pop();
		q2.pop();
	}
}
int main(){
	cin>>t;
	while(t--){
		cin>>n>>m;
		bfs();
	}
	return 0;
}
2023/12/16 10:58
加载中...