-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaekJoon1697.cpp
60 lines (54 loc) · 1.04 KB
/
BaekJoon1697.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
57
58
59
60
//BaekJoon 1697 숨바꼭질
#include <iostream>
#include <stdio.h>
#include <string>
#include <queue>
#include <utility>
using namespace std;
int n, k;
queue<pair<int, int>> qu;
bool visited[100001] = { false, };
pair<int, int> current;
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
if (n == k) {
cout << 0;
return 0;
}
qu.push(make_pair(n, 0));
visited[n] = true;
while (!qu.empty()) {
current = qu.front();
qu.pop();
int x = current.first;
int y = current.second;
if (x - 1 >= 0 && visited[x - 1] == false) {
if ((x - 1) == k) {
cout << y + 1;
return 0;
}
qu.push(make_pair(x - 1, y + 1));
visited[x - 1] = true;
}
if (x + 1 <= 100000 && visited[x + 1] == false) {
if ((x + 1) == k) {
cout << y + 1;
return 0;
}
qu.push(make_pair(x + 1, y + 1));
visited[x + 1] = true;
}
if (x * 2 <= 100000 && visited[x * 2] == false) {
if ((x * 2) == k) {
cout << y + 1;
return 0;
}
qu.push(make_pair(x * 2, y + 1));
visited[x * 2] = true;
}
}
return 0;
}