经典DP HDU 2084 数塔
2014-02-23 21:09
477 查看
经典DP HDU 2084 数塔
标签:算法dp
2014-02-23 21:09
997人阅读 评论(1)
收藏
举报
本文章已收录于:
![](http://static.blog.csdn.net/images/category_icon.jpg)
分类:
动态规划dp(52)
![](http://static.blog.csdn.net/images/arrow_triangle%20_down.jpg)
![](http://static.blog.csdn.net/images/arrow_triangle_up.jpg)
作者同类文章X
ACM 类题(45)
![](http://static.blog.csdn.net/images/arrow_triangle%20_down.jpg)
![](http://static.blog.csdn.net/images/arrow_triangle_up.jpg)
作者同类文章X
版权声明:本文为博主原创文章,未经博主允许不得转载。vasttian
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents
题目链接:http://acm.hdu.edu.cn/showproblem.PHP?pid=2084
这是一个经典的Dp问题!希望对你们有帮助!
在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的:
有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?
![](http://acm.hdu.edu.cn/data/images/2084-1.jpg)
已经告诉你了,这是个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
代码如下:
[cpp]
view plain
copy
print?
#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++)
{
4000
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;
}
![](http://static.blog.csdn.net/images/save_snippets.png)
#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; }
代码二:
[cpp]
view plain
copy
print?
#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
[i]);
}
printf("%d\n",maxx);
}
return 0;
}
![](http://static.blog.csdn.net/images/save_snippets.png)
#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 [i]); } printf("%d\n",maxx); } return 0; }
顶 5 踩 0
上一篇深入了解scanf/getchar/gets/cin等函数
下一篇矩阵快速幂4747
我的同类文章
动态规划dp(52)ACM 类题(45)
http://blog.csdn.net
•ZOJ 3872 Beauty of Array( DP思想 )2016-04-21阅读163
•动态规划(待更新)2016-04-16阅读118
•HDU 5492 Find a path(数学 + DP)2015-09-28阅读380
•HDU 3652 B-number(数位DP模板题)2015-09-25阅读242
•HDU 1520 & POJ 2342 Anniversary party(树形DP入门题)2015-09-03阅读278
•HDU 2476 String painter(区间DP啊)2015-08-30阅读511
•一个很特别的动态规划入门教程2016-04-16阅读226
•UESTC OJ 88 Fold The Paper(状态压缩啊)2015-10-07阅读394
•HDU 2089 不要62(数位DP啊)2015-09-26阅读215
•codeforces 392 B. Tower of Hanoi(汉诺塔 DP)2015-09-10阅读224
•HDU 5159 Card(DP+组合数啊)2015-09-02阅读254
更多文章
相关文章推荐
- hdu 2084(数塔-经典dp)
- hdu2084 - 数塔 (经典dp)
- 数塔 HDU - 2084【经典DP】
- hdoj 2084 数塔 【经典DP】
- HDU 2084 数塔 (经典dp)
- 动态规划K - 数塔 HDU - 2084 最经典入门
- HDOJ2084 数塔(经典的动态规划问题)
- HDU 2084 经典数塔问题
- HDU 2084 DP经典例子---数塔问题
- HDU 2084 数塔(经典dp)
- HDOJ_杭电2084_数塔问题,经典阶段DP动态规划问题
- hdoj 2084 数塔(经典dp)
- hdu 2084(数塔-经典dp)
- 各类经典dp问题分讲(HDU 2084、HDU 1003、HDU 1176)
- hdu 2084 数塔(水) 经典简单DP
- 横穿马路被电视台抓住采访时要沉着应答(绝对经典)『转贴』
- Gostop韩国经典花牌游戏玩法介绍
- 顶贴经典台词
- iolist跟list有什么区别?(经典)
- sqlserver sql语句|经典sql语句|实用sql语句