HDoj-2084-号码塔-dp

简介:

号码塔

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 22496    Accepted Submission(s): 13563


Problem Description
在讲述DP算法的时候,一个经典的样例就是数塔问题,它是这样描写叙述的:

有例如以下所看到的的数塔,要求从顶层走究竟层,若每一步仅仅能走到相邻的结点,则经过的结点的数字之和最大是多少?

已经告诉你了,这是个DP的题目,你能AC吗?
 

Input
输入数据首先包含一个整数C,表示測试实例的个数,每一个測试实例的第一行是一个整数N(1 <= N <= 100)。表示数塔的高度。接下来用N行数字表示数塔,当中第i行有个i个整数,且全部的整数均在区间[0,99]内。
 

Output
对于每一个測试实例,输出可能得到的最大和,每一个实例的输出占一行。
 

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

Sample Output
 
 
30
#include<cstdio> 
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
int a[110][110]={0};
using namespace std;
int main()
{
	
	int T,n,i,j;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		for(i=0;i<n;i++)
		{
			for(j=0;j<=i;j++)
			{
				scanf("%d",&a[i][j]);
			}
		}
		for(i=n-1;i>0;i--) 
	    { 
	       for(j=0;j<i;j++) 
		   { 
			 a[i-1][j]+=max(a[i][j],a[i][j+1]); 
		   }
	    }
		printf("%d\n",a[0][0]);
	}
	return 0;
} 


版权声明:本文博主原创文章,可能不会没有任何同意转载!







本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4888081.html,如需转载请自行联系原作者


相关文章
|
12月前
hdoj 1176免费馅饼(dp)
tmax = max(dp[t+1][i], dp[t+1][i-1]); else {
30 0
|
C++
【PAT甲级 - C++题解】1048 Find Coins
【PAT甲级 - C++题解】1048 Find Coins
51 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1068 Find More Coins
【PAT甲级 - C++题解】1068 Find More Coins
88 0
|
算法 C++
【PAT甲级 - C++题解】1072 Gas Station
【PAT甲级 - C++题解】1072 Gas Station
63 0
|
存储 C++
【PAT甲级 - C++题解】1075 PAT Judge
【PAT甲级 - C++题解】1075 PAT Judge
63 0
|
人工智能
UPC2021个人训练赛第39场 C: 粉兔找妹子(换根dp)
UPC2021个人训练赛第39场 C: 粉兔找妹子(换根dp)
91 0
UPC2021个人训练赛第39场 C: 粉兔找妹子(换根dp)
UPC-篮球运动(线性DP)
UPC-篮球运动(线性DP)
82 0
UPC-篮球运动(线性DP)
|
机器学习/深度学习 人工智能 JavaScript
LDUOJ——E. 塔(dp+状态的选择)
LDUOJ——E. 塔(dp+状态的选择)
82 0
|
机器学习/深度学习 算法
[UPC] Radio Prize | 换根dp
题目描述 All boring tree-shaped lands are alike, while all exciting tree-shaped lands are exciting in their own special ways.What makes Treeland more exciting than the other tree-shaped lands are the raddest radio hosts in the local area: Root and Leaf.
134 0
[UPC] Radio Prize | 换根dp