HDU 2084:数塔(动态规划)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2084

很简单的DP

#include <iostream>
#include <memory.h>
#define MAXHEIGHT 105
using namespace std;
int d[MAXHEIGHT][MAXHEIGHT];
int nums[MAXHEIGHT][MAXHEIGHT];
int cases, height;

int dp(int i, int j)
{
    if (d[i][j] >= 0)
        return d[i][j];
    return d[i][j] = i == height ? nums[i][j] : nums[i][j] + max(dp(i+1, j), dp(i+1, j+1));
}

int main()
{
    cin >> cases;
    while (cases--) {
        cin >> height;
        memset(d, -1, sizeof(d));
        for (int i = 0; i < height; ++i)
            for (int j = 0; j <= i; ++j)
                cin >> nums[i][j];
        cout << dp(0, 0) << endl;
    }
    return 0;
}
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页