zc
2024-08-08 16:04:42
发布于:广东
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
struct node{
int val;
int step;
};
node a[N];
int n, k;
int vis[N];
int main(){
cin >> n >> k;
node start = {n, 0};
vis[n] = 1;
queue<node> q;
q.push(start);
while(!q.empty()){
node head = q.front();
if(head.val == k){
cout << head.step;
return 0;
}
int x1 = head.val-1;
int x2 = head.val+1;
int x3 = head.val*2;
if(x1>=0 && x1<=100000 && vis[x1]==0){
node canGo = {x1,head.step+1};
vis[x1] = 1;
q.push(canGo);
}
if(x2>=0 && x2<=100000 && vis[x2]==0){
node canGo = {x2,head.step+1};
vis[x2] = 1;
q.push(canGo);
}
if(x3>=0 && x3<=100000 && vis[x3]==0){
node canGo = {x3,head.step+1};
vis[x3] = 1;
q.push(canGo);
}
q.pop();
}
return 0;
}
```cpp
```cpp
```cpp
这里空空如也
有帮助,赞一个