QWERYU
2024-08-08 16:03:01
发布于:广东
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
struct node{
int val;
int step;
}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 x = head.val - 1;
int y = head.val + 1;
int z = head.val * 2;
if(x >= 0 && x <= 100000 && vis[x] == 0){
node canGo = {x,head.step + 1};
vis[x] = 1;
q.push(canGo);
}
if(y >= 0 && y <= 100000 && vis[y] == 0){
node canGo = {y,head.step + 1};
vis[y] = 1;
q.push(canGo);
}
if(z >= 0 && z <= 100000 && vis[z] == 0){
node canGo = {z,head.step + 1};
vis[z] = 1;
q.push(canGo);
}
q.pop();
}
return 0;
}
这里空空如也
有帮助,赞一个