-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbj_13549.cpp
More file actions
56 lines (44 loc) · 985 Bytes
/
bj_13549.cpp
File metadata and controls
56 lines (44 loc) · 985 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <algorithm>
#include <utility>
#include <map>
#include <stack>
#include <queue>
#include <string.h>
#include <cstdlib>
#include <vector>
#include <math.h>
using namespace std;
int N, K;
int dp[200002] = {};
bool visit[200002] = {};
void BFS() {
queue<int> Q;
Q.push(N);
visit[N] = true;
while(!Q.empty()) {
int x = Q.front();
Q.pop();
int arr[3] = {
2*x, x-1, x+1
};
for(int i = 0; i<3; i++) {
if(visit[arr[i]] == false && arr[i] >= 0 && arr[i] <= 100000) {
dp[arr[i]] = dp[x] + 1;
if(i == 0)
dp[arr[i]] = dp[x];
visit[arr[i]] = true;
Q.push(arr[i]);
}
}
}
}
int main() {
cin.tie(NULL);
cout.tie(NULL);
ios_base::sync_with_stdio(false);
cin >> N >> K;
memset(visit, false, sizeof(visit));
BFS();
cout << dp[K];
}