-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13549.cpp
More file actions
39 lines (35 loc) · 783 Bytes
/
13549.cpp
File metadata and controls
39 lines (35 loc) · 783 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <queue>
using namespace std;
int N,K;
int graph[100001];
void bfs(){
queue<int> q;
q.push(N);
while(!q.empty()){
int v = q.front();
q.pop();
if (v+1<=100000)
if(graph[v+1]==0||graph[v+1]>graph[v]+1){
graph[v+1] = graph[v]+1;
q.push(v+1);
}
if (v-1>=0){
if(graph[v-1]==0||graph[v-1]>graph[v]+1){
graph[v-1] = graph[v]+1;
q.push(v-1);
}
}
if (v*2<=100000)
if(graph[v*2]==0||graph[v*2]>graph[v]){
graph[v*2] = graph[v];
q.push(v*2);
}
}
}
int main(){
cin >> N >> K;
graph[N]=1;
bfs();
cout<< graph[K]-1;
}