-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton Mash.cpp
More file actions
40 lines (33 loc) · 912 Bytes
/
Button Mash.cpp
File metadata and controls
40 lines (33 loc) · 912 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
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
const int MAX = 2 * n + 2;
vector<bool> visited(MAX, false);
queue<pair<int, int>> q;
q.push({ 0, 0 });
visited[0] = true;
while (!q.empty()) {
int x = q.front().first;
int steps = q.front().second;
q.pop();
if (x == n) {
cout << steps << endl;
break;
}
if (x + 1 < MAX && !visited[x + 1]) {
visited[x + 1] = true;
q.push({ x + 1, steps + 1 });
}
if (x - 1 >= 0 && !visited[x - 1]) {
visited[x - 1] = true;
q.push({ x - 1, steps + 1 });
}
if (x * 2 < MAX && !visited[x * 2]) {
visited[x * 2] = true;
q.push({ x * 2, steps + 1 });
}
}