#include<iostream>
#include<queue>
using namespace std;
#define MAX 100005
int N, K;
struct node {
int data;
int layer;
};
int a[MAX] = {0};
void bfs(int N, int K) {
queue<node> q;
node start;
start.data = N; start.layer = 0;
q.push(start);
while(!q.empty()) {
node temp = q.front();
q.pop();
if(temp.data == K) {
printf("%d\n", temp.layer);
break;
} else {
node temp1, temp2, temp3;
if(a[temp.data + 1] == 0){
temp1.data = temp.data + 1;
temp1.layer = temp.layer + 1;
q.push(temp1);
a[temp1.data] = 1;
}
if(a[temp.data - 1] == 0){
temp2.data = temp.data - 1;
temp2.layer = temp.layer + 1;
q.push(temp2);
a[temp2.data] = 1;
}
if(a[temp.data * 2] == 0 && temp.data * 2 < MAX){
temp3.data = temp.data * 2;
temp3.layer = temp.layer + 1;
q.push(temp3);
a[temp3.data] = 1;
}
}
}
}
int main() {
cin >> N >> K;
bfs(N, K);
return 0;
}
poj3258
最新推荐文章于 2019-04-08 19:17:00 发布