1 5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
30
/** * dp 模板题 * dp两个要素: * (1)最优子结构性质 * (2)子问题重叠性质 * 递归关系: dp[x][y]=max(dp[x-1][y-1],dp[x-1][y])+a[x][y]; 表示从顶层到达x层的第y个元素的数字和 */ #include <stdio.h> #include <string.h> #include <limits.h> #define maxn 105 int max(int a, int b){ if (a > b) return a; return b; } int main() { int t; scanf("%d", &t); while (t--) { int dp[maxn][maxn], a[maxn][maxn]; int n; scanf("%d", &n); for (int k = 1; k <= n; k++) for (int i = 1; i <= k; i++) scanf("%d", &a[k][i]); memset(dp, 0, sizeof(dp)); dp[1][1] = a[1][1]; for (int x = 2; x <= n; x++) for (int y = 1; y <= x; y++){ if (1 == y){ dp[x][y] = dp[x - 1][y] + a[x][y]; continue; } if (x == y){ dp[x][y] = dp[x - 1][y - 1] + a[x][y]; continue; } dp[x][y] = max(dp[x - 1][y - 1], dp[x - 1][y]) + a[x][y]; } int ans = -1; for (int i = 1; i <= n; i++){ if (ans < dp[n][i]) ans = dp[n][i]; } printf("%d\n", ans); } return 0; }
hdu 2085 数塔 -- dp模板题,布布扣,bubuko.com
原文地址:http://blog.csdn.net/u012964281/article/details/26166857