- Time: 2016-05-09-19:25:06
- Topic number: [2016-05-09][51nod][1002 number of tower to take number problem]
- Main topic:
- A triangle of positive integers with a height of n, from top to bottom, to calculate the number and maximum value. Each time you can only go to the next number of adjacent, for example, from the 3rd floor of 6 down, only to go to the 4th floor of 2 or 9.
- Analysis: Dynamic Planning
- DP[I][J] represents the maximum value to Dp[i][j]
- DP[I][J] = max (dp[i-1][j],dp[i-1][j-1]) + a[i][j];
- ans = max (dp[n-1][i])
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 500 + 10;
int dp[maxn][maxn],a[maxn][maxn];
int main(){
int n;
scanf("%d",&n);
memset(dp,0,sizeof(dp));
for(int i = 0 ; i < n ; ++i){
for(int j = 0 ; j <= i ; ++j){
scanf("%d",&a[i][j]);
}
}
dp[0][0] = a[0][0];
int ans = 0;
for ( int i = 1 ; I < n ++ i ) {
DP [ i ][ 0 = DP [ i - 1 ][ 0 ] + a [ i ][ 0 ];
ans = max(ans , dp[i][0]);
for(int j = 1 ; j <= i ; ++j){
DP[I][J] =Max(DP[I- 1][J] +a[I][J] ,DP[I- 1][J- 1] +a[I][J]);
ans = max(dp[i][j] , ans);
}
}
printf("%d\n",ans);
return 0;
}
From for notes (Wiz)
[2016-05-09] [51nod] [1002 number of tower fetching questions]