经典DP HDU 2084 数塔
来源:互联网 发布:森林优化补丁 编辑:程序博客网 时间:2024/05/16 18:47
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084
这是一个经典的Dp问题!希望对你们有帮助!
在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的:
有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?
已经告诉你了,这是个DP的题目,你能AC吗?
有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?
已经告诉你了,这是个DP的题目,你能AC吗?
Input
输入数据首先包括一个整数C,表示测试实例的个数,每个测试实例的第一行是一个整数N(1 <= N <= 100),表示数塔的高度,接下来用N行数字表示数塔,其中第i行有个i个整数,且所有的整数均在区间[0,99]内。
Output
对于每个测试实例,输出可能得到的最大和,每个实例的输出占一行。
Sample Input
1573 88 1 0 2 7 4 44 5 2 6 5
Sample Output
30
代码如下:
#include <cstdio>#include <iostream>using namespace std;int max(int a, int b){ return a > b ? a:b;}int main(){ int n; int dp[101][101]; int t; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i = 0; i < n; i++) { for(int j = 0; j <= i; j++) { scanf("%d",&dp[i][j]); } } for(int i = n-2; i >= 0; i--) { for(int j = 0; j <= i; j++) { dp[i][j] += max(dp[i+1][j],dp[i+1][j+1]); } } printf("%d\n",dp[0][0]); } return 0;}
代码二:
#include <cstdio>#include <cmath>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define maxn 1010int dp[130][maxn];int main(){ int t; int n, m; scanf("%d",&t); while(t--) { scanf("%d",&n); memset(dp,0,sizeof(dp)); for(int i = 1; i <= n; i++) { for(int j = 1; j <= i; j++) { scanf("%d",&dp[i][j]); } } for(int i = 1; i <= n; i++) { for(int j = 1; j <= i; j++) { dp[i][j]+=max(dp[i-1][j],dp[i-1][j-1]); } } int maxx = 0; for(int i = 1; i <= n; i++) { maxx = max(maxx,dp[n][i]); } printf("%d\n",maxx); } return 0;}
5 0
- 经典DP HDU 2084 数塔
- hdu 2084(数塔-经典dp)
- HDU 2084 数塔 (经典dp)
- HDU 2084 数塔(经典dp)
- 经典DP HDU 2084 数塔
- hdu 2084 数塔(水) 经典简单DP
- HDU 数塔 (dp入门经典)
- HDU 2084 数塔 dp
- hdu 2084 数塔 dp
- HDU 2084 数塔 DP
- HDU 2084 数塔(dp)
- HDU 2084 数塔 DP
- hdu 2084 数塔(dp)
- HDU 2084 数塔(DP)
- HDU 2084 数塔(DP)
- hdu 2084 数塔 dp
- HDU 2084 数塔 (DP)
- HDU-2084-数塔【DP】
- 创业之初需要准备的六个方面
- 数据结构——二叉树的遍历问题(C语言)
- 窗口透明化 AlphaBlend
- 千万当心这10所“野鸡大学”,由于这些学校与正牌知名大学的名字极易混淆,学生们需特别提防。
- Michael和Sophia的英语之路
- 经典DP HDU 2084 数塔
- Servlet开发技术基础
- ZOJ 3469 Food Delivery
- 实例对比Stack类与Queue类
- 面向对象方法开发的特点
- 设计模式
- Oracle中的表分区
- Mongodb 自动增长 自增id 实现
- X宝外包软件测试工程师面试经历