#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define max 100
int a[max][max];
int opt[max][max];
int sum(int i,int j,int n)
{
if (opt[i][j]!=0) return opt[i][j];
if( i==n-1 ) { opt[i][j]=a[i][j];return a[i][j];}
else
{ opt[i+1][j]=sum(i+1,j,n);
opt[i+1][j+1]=sum(i+1,j+1,n);
if (opt[i+1][j]>opt[i+1][j+1])
return opt[i+1][j]+a[i][j];
else
return opt[i+1][j+1]+a[i][j];
}
}
int main()
{
int i,j,t; int n;
scanf("%d",&n);
memset(opt,0,sizeof(opt));
for (i=0;i<n;i++)
for(j=0;j<=i;j++)
scanf("%d",&a[i][j]);
printf("%d\n",sum(0,0,n));
return 0;
}
数字三角形DP
最新推荐文章于 2024-03-29 11:57:04 发布