-
Notifications
You must be signed in to change notification settings - Fork 0
/
13549_숨바꼭질3.cpp
56 lines (44 loc) · 897 Bytes
/
13549_숨바꼭질3.cpp
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 <math.h>
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define MAX 100001
int res = MAX;
int n, k;
bool visited[MAX] = {
false,
};
priority_queue<pair<int, int>, vector<pair<int, int> >,
greater<pair<int, int> > >
pq;
void bfs() {
while (!pq.empty()) {
int time = pq.top().first;
int x = pq.top().second;
pq.pop();
visited[x] = true;
if (x == k) {
res = min(res, time);
break;
}
if (x - 1 >= 0 && !visited[x - 1]) {
pq.push(make_pair(time + 1, x - 1));
}
if (x + 1 < MAX && !visited[x + 1]) {
pq.push(make_pair(time + 1, x + 1));
}
if (2 * x < MAX && !visited[2 * x]) {
pq.push(make_pair(time, x * 2));
}
}
return;
}
int main() {
cin >> n >> k;
pq.push(make_pair(0, n));
bfs();
cout << res;
}