bfs宽搜
Problem - B - Codeforceshttps://i-blog.csdnimg.cn/direct/6a90ff6542c343fa9cdf99483bf3c622.png
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N=1e4+10;
int q, hh,tt;
int d;
int n,m;
int bfs(int u)
{
memset(d,-1,sizeof d);
q=u;
d=0;
while(hh<=tt){
int t=q;
if(t==m) return d;
int j=t-1;
if(j>=0 && j<=10000 && d==-1){
q[++tt]=j;
d=d+1;
}
j=t*2;
if(j>=0 && j<=10000 && d==-1){
q[++tt]=j;
d=d+1;
}
}
}
void solve()
{
cin>>n>>m;
int cnt=0;
if(n>=m) cnt=n-m;
else {
cnt=bfs(n);
}
cout<<cnt<<'\n';
}
int main()
{
int T;
// cin>>T;
T=1;
while(T--){
solve();
}
return 0;
}
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]