Catch That Cow S
注意理解BFS记录每个点步数的操作
#include <bits/stdc++.h>
using namespace std;
int x,y,dis[100000],t;
int BFS()
{
queue<int> q;
q.push(x);
int dis[100000];
memset(dis,0x7fffffff,sizeof(dis));
dis[x]=0;
while(!q.empty())
{
int temp=q.front();
q.pop();
if(temp==y)
return dis[temp];
if(temp+1<=100000&&dis[temp+1]==dis[0])
{
dis[temp+1]=dis[temp]+1;
q.push(temp+1);
}
if(temp-1>0&&dis[temp-1]==dis[0])
{
dis[temp-1]=dis[temp]+1;
q.push(temp-1);
}
if(temp*2<=100000&&dis[temp*2]==dis[0])
{
dis[temp*2]=dis[temp]+1;
q.push(temp*2);
}
}
return -1;
}
int main()
{
for(cin>>t;t;t--)
{
cin>>x>>y;
cout<<BFS()<<endl;
}
return 0;
}