|Tyvj|动态规划|P1044 数字三角形

http://tyvj.cn/p/1044
简单的dp,不做解释了

参考代码:

#include
#include
#include
using namespace std;
int main () 
{
	const int maxn = 25 + 5;
	int n;
	int a[maxn][maxn];
	scanf("%d", &n);
	for (int i=1;i<=n;i++) 
	{
	   for (int j=1;j<=i;j++) 
	   {
			scanf("%d", &a[i][j]);
	   }
	}
	int d[maxn][maxn];
	for (int i=1;i<=n;i++) d[n][i] = a[n][i];
	for (int i=n-1;i>0;i--) 
	{
		for (int j=1;j<=i;j++) 
		{
			d[i][j] = max(d[i+1][j]+a[i][j], d[i+1][j+1]+a[i][j]);
		}
	}
	printf("%d\n", d[1][1]);
	return 0;
}


你可能感兴趣的:(tyvj,动态规划)