题目给的范围是100000(5个0)QAQ
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
#define MAX 100000
int N,K;
int visited[100010];
struct steps{
int x;
int s;
steps(int cx,int cs):x(cx),s(cs) { }
};
queue<steps> q;
int main(){
cin >> N >> K;
memset(visited,0,sizeof(visited));
q.push(steps(N,0));
visited[N] = 1;
while(!q.empty()){
steps step = q.front();
if(step.x == K){
cout << step.s << endl;
return 0;
}
else{
if(step.x - 1 >= 0 && !visited[step.x - 1]){
q.push(steps(step.x - 1,step.s + 1));
visited[step.x - 1] = 1;
}
if(step.x + 1 <= MAX && !visited[step.x + 1]){
q.push(steps(step.x + 1,step.s + 1));
visited[step.x + 1] = 1;
}
if(step.x * 2 <= MAX && !visited[step.x * 2]){
q.push(steps(step.x * 2,step.s + 1));
visited[step.x * 2] = 1;
}
q.pop();
}
}
return 0;
}