forked from b-chae/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1697.cpp
More file actions
59 lines (48 loc) · 939 Bytes
/
Copy path1697.cpp
File metadata and controls
59 lines (48 loc) · 939 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
57
58
59
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
int N, K;
bool visited[100002];
int level[100002];
list<int> bfsQueue;
int ans;
void bfs() {
int x = bfsQueue.front();
bfsQueue.pop_front();
if (x == K) {
ans = level[x];
return;
}
if (x - 1 == K || x + 1 == K || 2 * x == K) {
ans = level[x] + 1;
return;
}
if (x > 0 && !visited[x - 1]) {
visited[x - 1] = true;
level[x - 1] = level[x] + 1;
bfsQueue.push_back(x - 1);
}
if (x < 100001 && !visited[x + 1]) {
visited[x + 1] = true;
level[x + 1] = level[x] + 1;
bfsQueue.push_back(x + 1);
}
if (x < 50001 && !visited[2 * x]) {
visited[2 * x] = true;
level[2 * x] = level[x] + 1;
bfsQueue.push_back(2 * x);
}
bfs();
}
int main()
{
int i;
for (i = 0; i < 100002; i++) visited[i] = false;
for (i = 0; i < 100002; i++) level[i] = 0;
cin >> N >> K;
bfsQueue.push_back(N);
bfs();
cout << ans;
}