-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbj_1697.cpp
More file actions
48 lines (40 loc) · 861 Bytes
/
bj_1697.cpp
File metadata and controls
48 lines (40 loc) · 861 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
#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[100002] = {};
bool visit[100002] = {};
void BFS() {
queue<int> Q;
visit[N] = true;
Q.push(N);
while(!Q.empty()) {
int x = Q.front();
Q.pop();
int arr[3] = {x+1, x-1, 2*x};
for(int i = 0; i<3; i++) {
if(visit[arr[i]] == false && arr[i]>= 0 && arr[i] <= 100000) {
dp[arr[i]] = dp[x] + 1;
visit[arr[i]] = true;
Q.push(arr[i]);
}
}
}
}
int main() {
cin.tie(NULL);
cout.tie(NULL);
ios_base::sync_with_stdio(fals);
cin >> N >> K;
memset(visit, false, sizeof(visit));
BFS();
cout << dp[K];
}e