-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1697.cpp
More file actions
50 lines (43 loc) · 929 Bytes
/
1697.cpp
File metadata and controls
50 lines (43 loc) · 929 Bytes
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
#include <iostream>
#include <queue>
using namespace std;
#define MAXSIZE 100004
int main() {
int n, k, act_1, act_2, act_3, cnt = 0;
queue<pair<int, int>> q;
bool check[MAXSIZE] = { 0, };
cin >> n >> k;
q.push({ n, cnt });
check[n] = true;
if (n == k) {
cnt = 0;
}
else {
while (!q.empty()) {
act_1 = q.front().first + 1;
act_2 = q.front().first - 1;
act_3 = q.front().first * 2;
cnt = q.front().second + 1;
q.pop();
if ((act_1 == k) || (act_2 == k) || (act_3 == k)) {
break;
}
else {
if ((check[act_1] != true) && (act_1 <= 100000)) {
q.push({ act_1,cnt });
check[act_1] = true;
}
if ((act_2 >= 0) && (check[act_2] != true)) {
q.push({ act_2,cnt });
check[act_2] = true;
}
if ((act_3 >= 0) && (act_3 <= 100000) && (check[act_3] != true)) {
q.push({ act_3,cnt });
check[act_3] = true;
}
}
}
}
cout << cnt;
return 0;
}