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
|
#include <iostream>
#include <vector>
#define INF 987654321
#define MAXX 1001
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
int map[MAXX] = {0};
for (int i = 0; i < n; i++) {
cin >> map[i];
}
int dp[MAXX] = {0}; //이전 단계에서 점프해서 도착했을 때 최소 횟수를 저장
for (int i = 0; i < n; i++) {
dp[i] = INF;
}
dp[0] = 0;
for (int i = 0; i < n; i++) {
if (map[i] == 0) continue;
for (int j = 1; j <= map[i]; j++) {
if (i + j >= n) break;
dp[i+j] = min(dp[i+j], dp[i]+1);
}
}
// for (int i = 0; i < n; i++) cout << dp[i] << ' ';
// cout << '\n';
if (dp[n-1] == INF) cout << -1 << '\n';
else
cout << dp[n-1] << '\n';
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
BOJ/C++