✅ 使用动态规划解决跳木桩问题
This commit is contained in:
parent
04fa87a8da
commit
710ee8853c
19
jumping-wooden-trunk/main.cc
Normal file
19
jumping-wooden-trunk/main.cc
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
const int MAXN = 105;
|
||||||
|
int dp[MAXN], arr[MAXN];
|
||||||
|
int main() {
|
||||||
|
int n;
|
||||||
|
cin >> n;
|
||||||
|
for (int i = 1; i <= n; i++) {
|
||||||
|
cin >> arr[i];
|
||||||
|
dp[i] = n - 1;
|
||||||
|
}
|
||||||
|
dp[1] = 0;
|
||||||
|
for (int i = 1; i <= n; i++) {
|
||||||
|
for (int j = i + 1; j <= i + arr[i] && j <= n; j++) {
|
||||||
|
dp[j] = min(dp[j], dp[i] + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cout << dp[n] << endl;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user