听取WA声一片
查看原帖
听取WA声一片
1265432
Liuhx7871楼主2024/4/26 19:56
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=200005;
int a,b,vis[maxn],step[maxn];
void bfs()
{
    memset(vis,0,sizeof vis);
	queue<int>q;
	q.push(a);
	vis[a]=1;
	while(!q.empty())
	{
		
		int x=q.front();
		q.pop();
		if(x==b)
		{
			cout<<step[b]<<endl;
			return ;
		}
		if(!vis[x*2]&&x*2<=b)
		{
			q.push(2*x);
			vis[2*x]=1;
			step[2*x]=step[x]+1;
		}
		 if(!vis[x-1]&&x-1!=a&&x-1>0)
		 {
		 	q.push(x-1);
			 vis[x-1]=1;
			 step[x-1]=step[x]+1; 
		 }
		if(!vis[x+1]&&x+1<=b)
		{
			q.push(x+1);
			vis[x+1]=1;
			step[x+1]=step[x]+1;
		 } 
		 
		
	}
}
int main()
{
	int t;
	cin>>t;
	while(t--){
		cin>>a>>b;
	bfs();
	}
	return 0;
}
2024/4/26 19:56
加载中...