-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaekjoon13549.cpp
More file actions
54 lines (42 loc) · 774 Bytes
/
baekjoon13549.cpp
File metadata and controls
54 lines (42 loc) · 774 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
#include <iostream>
#include <queue>
using namespace std;
int N, K;
deque<int> q;
int arr[100001], xmove[2] = { 1, -1 };
void BFS(int start, int target) {
arr[start] = 0;
q.push_back(start);
while (!q.empty()) {
int x = q.front();
q.pop_front();
if (x == target) {
return;
}
int nextX = 0;
nextX = 2 * x;
if (nextX >= 0 && nextX <= 100000) {
if (arr[nextX] == -1) {
arr[nextX] = arr[x];
q.push_front(nextX);
}
}
for (int i = 0; i < 2; i++) {
nextX = x + xmove[i];
if (nextX >= 0 && nextX <= 100000) {
if (arr[nextX] == -1) {
arr[nextX] = arr[x] + 1;
q.push_back(nextX);
}
}
}
}
}
int main() {
cin >> N >> K;
for (int i = 0; i <= 100000; i++) {
arr[i] = -1;
}
BFS(N, K);
cout << arr[K];
}