-
Notifications
You must be signed in to change notification settings - Fork 5
/
1697.cpp
46 lines (37 loc) · 911 Bytes
/
1697.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
#include <iostream>
#include <queue>
using namespace std;
int time_to_get[100000]; // 특정 점부터 시작해서 i로 가는데 걸리는 최소 시간
void Solution1697() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int start, dest;
cin >> start >> dest;
queue<int> q;
q.push(start);
time_to_get[start] = 1;
while (!q.empty())
{
int cur = q.front();
q.pop();
if (cur - 1 >= 0 && time_to_get[cur - 1] == 0) {
time_to_get[cur - 1] = time_to_get[cur] + 1;
q.push(cur - 1);
}
if (cur + 1 <= 100000 && time_to_get[cur + 1] == 0) {
time_to_get[cur + 1] = time_to_get[cur] + 1;
q.push(cur + 1);
}
if (cur * 2 <= 100000 && time_to_get[cur * 2] == 0) {
time_to_get[cur * 2] = time_to_get[cur] + 1;
q.push(cur * 2);
}
if (time_to_get[dest] != 0) {
break;
}
}
cout << time_to_get[dest] -1<< "\n";
}
int main(void) {
Solution1697();
return 0;
}