-
Notifications
You must be signed in to change notification settings - Fork 4
/
Line_5번.cpp
66 lines (56 loc) · 1.14 KB
/
Line_5번.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
61
62
63
64
65
66
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
void line5() {
queue<int> q;
int min = 0;
int max = 200001;
int cony, brown;
cin >> cony;
cin >> brown;
int n = 1;
while (cony + (n * (n + 1) / 2) <= max)
n++;
vector<int> cony_pos(n);
vector<bool> check(max);
vector<int> brown_pos(max);
cony_pos[0] = cony;
for (int i = 1; i < n; i++)
cony_pos[i] = cony_pos[i - 1] + i;
check[brown] = true;
brown_pos[brown] = 0;
q.push(brown);
while (!q.empty()) {
int now = q.front();
q.pop();
if (now - 1 >= min) {
if (check[now - 1] == false) {
q.push(now - 1);
check[now - 1] = true;
brown_pos[now - 1] = brown_pos[now] + 1;
}
}
if (now + 1 <= max) {
if (check[now + 1] == false) {
q.push(now + 1);
check[now + 1] = true;
brown_pos[now + 1] = brown_pos[now] + 1;
}
}
if (now * 2 <= max) {
if (check[now * 2] == false) {
q.push(now * 2);
check[now * 2] = true;
brown_pos[now * 2] = brown_pos[now] + 1;
}
}
}
for (int i = 0; i < n; i++) {
if (brown_pos[cony_pos[i]] == i) {
cout << brown_pos[cony_pos[i]];
return;
}
}
cout << -1;
}