题解
2024-08-08 16:30:14
发布于:浙江
7阅读
0回复
0点赞
不准抄
#include <bits/stdc++.h>
using namespace std;
struct node{
int x,t;
};
queue<node>que;
int n,k;
bool vis[100010];
int bfs(){
while (!que.empty()){
node a=que.front();
que.pop();
if (a.x==k) return a.t;
if (a.x-1>=0 &&a.x-1<=100000 &&vis[a.x-1]==0){
que.push({a.x-1,a.t+1});
vis[a.x-1]=1;
}
if (a.x+1>=0 &&a.x+1<=100000 &&vis[a.x+1]==0){
que.push({a.x+1,a.t+1});
vis[a.x+1]=1;
}
if (a.x*2>=0 &&a.x*2<=100000 &&vis[a.x*2]==0){
que.push({a.x*2,a.t+1});
vis[a.x*2]=1;
}
}
}
int main(){
cin>>n>>k;
que.push({n,0});
vis[n]=1;
cout<<bfs();
return 0;
}
这里空空如也
有帮助,赞一个