-
Notifications
You must be signed in to change notification settings - Fork 0
/
숨바꼭질 3.cc
48 lines (39 loc) · 994 Bytes
/
숨바꼭질 3.cc
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 <cstring>
#include <queue>
using namespace std;
int N, K, answer = 0;
int visited[200004];
void bfs(int N) {
queue<int> q;
q.push(N);
visited[N] = 0;
while(!q.empty()) {
int cur = q.front();
q.pop();
if (cur == K) {
cout << visited[cur];
break;
}
if (2 * cur <= 200000 && visited[2 * cur] == -1) {
q.push(2 * cur);
visited[2 * cur] = visited[cur];
}
if (cur - 1 >= 0 && visited[cur - 1] == -1) {
q.push(cur - 1);
visited[cur - 1] = visited[cur] + 1;
}
if (cur + 1 <= 200000 && visited[cur + 1] == -1) {
q.push(cur + 1);
visited[cur + 1] = visited[cur] + 1;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> K;
memset(visited, -1, sizeof(visited));
bfs(N);
return 0;
}