#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<pair<int, int>>> position;
vector<int> DP;
int minimumJumps (int l, int r, int idx, vector<bool> & visited, int jumps) {
// base case
if (DP[idx] != -1) {
return DP[idx];
}
visited[idx] = true;
// visit all posible conections
for (auto conx: position[idx]) {
int doge = conx.first, p = conx.second;
if (p == 0) {
continue;
}
// to the left
int left = idx - p;
for (int i = 1; l <= left; i++) {
if (!position[left].empty() && !visited[left]) {
jumps = min(jumps, i + minimumJumps(l, r, left, visited, jumps));
}
left -= p;
}
// to the right
int right = idx + p;
for (int i = 1; right <= r; i++) {
if (!position[right].empty() && !visited[right]) {
jumps = min(jumps, i + minimumJumps(l, r, right, visited, jumps));
}
right += p;
}
}
// build the DP
return DP[idx] = jumps;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, m, x, p, answer, pos0;
cin >> n >> m;
position.assign(n, vector<pair<int, int>>());
DP.assign(n, -1);
vector<bool> visited(n, false);
// doge 0
cin >> x >> p;
position[x].push_back(make_pair(0, p));
pos0 = x;
// doge 1
cin >> x >> p;
position[x].push_back(make_pair(1, p));
DP[x] = 0;
// other doges
m -= 2;
for (int i = 0; i < m; i++) {
cin >> x >> p;
position[x].push_back(make_pair(i, p));
}
// call function
answer = minimumJumps(0, n-1, pos0, visited, 1e9);
if (answer == 1e9) {
cout << "-1\n";
} else {
cout << answer << '\n';
}
return 0;
}
Compilation message
skyscraper.cpp: In function 'int minimumJumps(int, int, int, std::vector<bool>&, int)':
skyscraper.cpp:18:13: warning: unused variable 'doge' [-Wunused-variable]
18 | int doge = conx.first, p = conx.second;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
392 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |