数字金字塔


Description

你和权权是一对很好很好的朋友。有一天,你们无聊得很,便上网冲浪,突然在一个叫做USACO的网中找到了一个游戏:《数字金子塔》。游戏规则是这样的:求一个数字金字塔中从最高点开始在底部任意处结束的路径经过数字的和的最大,其中的每一步可以走到下方的点也可以到达右下方的点。例如在下面的例子中,从7 — 3 — 8 — 7 –- 5的路径产生了最大和:30。
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
你们便约定了谁能计算出最后的值便是赢者。你仰天(天花板)长叹:我能成为赢者吗,要知道权权可是很厉害的哦……

Input

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

Output

30

Hink

Time Limit:10000MS
Memory Limit:65536K

解题思路

使用递推找出最好的路线

#include<iostream>
#include<iomanip>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int a[1005][1005],b[1005][1005],n,t=0;
int DP(int x,int y)
{
   
	if(x>n)return 0;
	if(a[x][y]!=0) return a[x][y];
	if(x==n) a[x][y]=b[x][y];
	else a[x][y]=max(DP(x+1,y),DP(x+1,y+1))+b[x][y];//递推
	return a[x][y];
} 
int main()
{
   
	cin>>n;
	for(int i=1;i<=n;i++)
	{
   
		for(int j=1;j<=i;j++)
		{
   
			scanf("%d",&b[i][j]);
		}
	}
	printf("%d",DP(1,1));
	return 0;
}