HDU-2084 数塔 动态规划

#include <cstdlib>
#include <cstring>
#include <cstdio>
#define MAXN 105
using namespace std;

int N, seq[MAXN][MAXN], dp[MAXN][MAXN];

inline int max(int x, int y)
{
    return x > y ? x : y;
}

int DP()
{ 
    for (int i = 1; i <= N; ++i) {
        dp[N][i] = seq[N][i];  
    }
    for (int i = N-1; i >= 1; --i) {
        for (int j = 1; j <= i; ++j) {
            dp[i][j] = max(dp[i+1][j], dp[i+1][j+1]) + seq[i][j];
        }
    } 
    return dp[1][1];
}

int main()
{
    int T;
    scanf("%d", &T);
    while (T--) {
        scanf("%d", &N);
        for (int i = 1; i <= N; ++i) {
            for (int j = 1; j <= i; ++j) {
                scanf("%d", &seq[i][j]);
            }
        }
        printf("%d\n", DP());
    }    
    return 0;
}
posted @ 2012-04-19 17:00  沐阳  阅读(294)  评论(0编辑  收藏  举报