数字三角形,DP裸题
#include<stdio.h>
#include<string.h>
#define max(a,b) (a)>(b)?a:b int g[][],dp[][]; int max1(int i,int j){
return i>j?i:j;
} int main(){
int C;
while(scanf("%d",&C)!=EOF){
for(int q=;q<=C;q++){
int N;
scanf("%d",&N);
memset(dp,,sizeof(dp));
int i,j;
for(i=;i<=N;i++){
for(j=;j<=i;j++){
scanf("%d",&g[i][j]);
}
}
dp[][]=g[][];
for(i=;i<=N;i++){
for(j=;j<=i;j++){
if(j==)dp[i][j]=dp[i-][j]+g[i][j];
else if(j==i)dp[i][j]=dp[i-][j-]+g[i][j];
else dp[i][j]=max1(dp[i-][j-],dp[i-][j])+g[i][j];
}
}
int m=dp[N][];
for(i=;i<=N;i++)if(dp[N][i]>m)m=dp[N][i];
printf("%d\n",m);
} }
return ;
}