http://acm.hdu.edu.cn/showproblem.php?pid=2084
状态转移方程:
dp[i][j] = MAX(dp[i+1][j],dp[i+1][j+1])+tower[i][j]
#include <iostream> #include <string> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath> #include <algorithm> #include <stack> using namespace std; #define MEM(a,b) memset(a,b,sizeof(a)) #define pf printf #define sf scanf #define debug printf("!/m") #define L 150 #define MAX(a,b) a>b?a:b int tower[L][L]; int dp[L][L]; int main() { int t,i,j; sf("%d",&t); while(t--) { int n; sf("%d",&n); MEM(dp,0); for(i = 0;i<n;i++) { for(j = 0;j<=i;j++) { sf("%d",&tower[i][j]); } } for(i = n-1;i>=0;i--) { for(j = 0;j<=i;j++) { int t = MAX(dp[i+1][j],dp[i+1][j+1]); dp[i][j] = t+tower[i][j]; } } pf("%d ",dp[0][0]); } return 0; }