经典DP HDU 2084 数塔
来源:互联网 发布:手机淘宝官网首页 编辑:程序博客网 时间:2024/04/30 05:26
经典DP HDU 2084 数塔
标签: 算法dp
2014-02-23 21:09 997人阅读 评论(1)收藏举报
本文章已收录于:
分类:
作者同类文章X
作者同类文章X
版权声明:本文为博主原创文章,未经博主允许不得转载。vasttian
转载请注明出处: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 1010
- int 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
- 上一篇深入了解scanf/getchar/gets/cin等函数
- 下一篇矩阵快速幂4747
我的同类文章
http://blog.csdn.net
- •ZOJ 3872 Beauty of Array( DP思想 )2016-04-21
- •动态规划(待更新)2016-04-16
- •HDU 5492 Find a path(数学 + DP)2015-09-28
- •HDU 3652 B-number(数位DP模板题)2015-09-25
- •HDU 1520 & POJ 2342 Anniversary party(树形DP入门题)2015-09-03
- •HDU 2476 String painter(区间DP啊)2015-08-30
- •一个很特别的动态规划入门教程2016-04-16
- •UESTC OJ 88 Fold The Paper(状态压缩啊)2015-10-07
- •HDU 2089 不要62(数位DP啊)2015-09-26
- •codeforces 392 B. Tower of Hanoi(汉诺塔 DP)2015-09-10
- •HDU 5159 Card(DP+组合数啊)2015-09-02
0 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】
- windows内核情景分析--窗口消息
- Agent不能同步master的解决方法
- Hadoop源码之----hadoop-common-project(一、工程目录结构)
- win7一键拖动生成快速启动栏
- 10.字符设备驱动模型(1)-使用字符驱动程序
- 经典DP HDU 2084 数塔
- 字符设备控制
- 电脑蓝屏重启后,studio无法认出Android环境 setup JDK 的解决办法(转载)
- Spring4 实战笔记(3):面向切面编程
- HTML调用PHP的方法
- TODO:macOS上ThinkPHP5和Semantic-UI集成
- 一些过程中可能会用到的工具之类的
- css样式分离合并与模块化
- 4-12 二叉搜索树的操作集 (30分)