hdu 2084 数塔

入门水题,做一做涨涨自信还是很好的。。。

题目链接:https://vjudge.net/problem/HDU-2084

#include 
#include 
#include 

using namespace std;

const int maxn = 105;
int G[maxn][maxn];
int n;
int dp[maxn][maxn];
int f[2] = {0,-1};

void solve () {
    dp[1][1] = G[1][1];
    for (int i = 2 ; i <= n ; i++) {
        for (int j = 1 ; j <= i ; j++) {
            for (int k = 0 ; k < 2 ; k++) {
                int tx = i-1;
                int ty = j+f[k];
                if (ty >= 1  && ty <= tx) {
                    dp[i][j] = max(dp[i][j] , dp[tx][ty]+G[i][j]);
                }
            }
        }
    }
    int ans = 0;
    for (int i = 1;  i <= n ; i++)
        ans = max(ans , dp[n][i]);
    printf("%d\n" , ans);
}

int main () {
    int ncase;
    scanf("%d" , &ncase);
    while (ncase--) {
        memset(dp,0,sizeof(dp));
        memset(G,0,sizeof(G));
        scanf("%d" , &n);
        for(int i = 1 ; i <= n ; i++) 
            for (int j = 1 ; j <= i ; j++) 
                scanf("%d" , &G[i][j]);
        solve();
    }
}


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