标签:
由下往上推,状态转移方程:dp[i][j] = max( dp[i+1][j], dp[i+1][j+1] ) + a[i][j]
#include <iostream> #include <cstdio> using namespace std; int dp[105][105]; int a[105][105]; int max( int a, int b ) { return a > b ? a : b; } int main() { int cas; int n; scanf( "%d", &cas ); while( cas-- ) { scanf( "%d", &n ); for( int i = 0; i < n; i++ ) { for( int j = 0; j < i + 1; j++ ) scanf( "%d", &a[i][j] ); } for( int i = 0; i < n; i++ ) dp[n-1][i] = a[n-1][i]; for( int i = n - 2; i >= 0; i-- ) { for( int j = 0; j <= i; j++ ) dp[i][j] = max( dp[i+1][j], dp[i+1][j+1] ) + a[i][j]; } printf( "%d\n", dp[0][0] ); } return 0; }
标签:
原文地址:http://www.cnblogs.com/fengxy/p/4792694.html