hdu 2084 简单dp

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX 107
using namespace std;

int dp[MAX][MAX];
int a[MAX][MAX];

int main ( )
{
    int t,n;
    scanf ( "%d" , &t );
    while ( t-- )
    {
        scanf ( "%d" , &n );
        for ( int i = 1 ; i <= n ; i++ )
            for ( int j = 1 ; j <= i ; j++ )
                scanf ( "%d" , &a[i][j] );
        int ans = 0;
        memset ( dp , 0 , sizeof ( dp ) );
        dp[1][1] = a[1][1];
        for ( int i = 1 ; i < n ; i++ )
            for ( int j = 1 ; j <= i ;j++ )
            {
                dp[i+1][j] = max ( dp[i][j] + a[i+1][j] , dp[i+1][j] );
                dp[i+1][j+1] = max ( dp[i][j] + a[i+1][j+1] , dp[i+1][j+1] );
            }
        for ( int i = 1 ; i <= n ; i++ )
            ans = max ( dp[n][i] , ans ) ;
        printf ( "%d\n" , ans );
    }
}

你可能感兴趣的:(C++,动态规划)