经典DP HDU 2084 数塔

来源:互联网 发布:手机淘宝官网首页 编辑:程序博客网 时间:2024/04/30 05:26

经典DP HDU 2084 数塔

标签: 算法dp
997人阅读 评论(1)收藏举报
本文章已收录于:
分类:
作者同类文章X
    作者同类文章X

      转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents

      题目链接:http://acm.hdu.edu.cn/showproblem.PHP?pid=2084



      这是一个经典的Dp问题!希望对你们有帮助!

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

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

      已经告诉你了,这是个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


      代码如下:

      [cpp] view plain copy
      print?
      1. #include <cstdio>  
      2. #include <iostream>  
      3. using namespace std;  
      4. int max(int a, int b)  
      5. {  
      6.     return a > b ? a:b;  
      7. }  
      8. int main()  
      9. {  
      10.     int n;  
      11.     int dp[101][101];  
      12.     int t;  
      13.     scanf("%d",&t);  
      14.     while(t--)  
      15.     {  
      16.         scanf("%d",&n);  
      17.         for(int i = 0; i < n; i++)  
      18.         {  
      19.             for(int j = 0; j <= i; j++)  
      20.             {  
      21.                 scanf("%d",&dp[i][j]);  
      22.             }  
      23.         }  
      24.   
      25.         for(int i = n-2; i >= 0; i--)  
      26.         {  
      27.             for(int j = 0; j <= i; j++)  
      28.             {  
      29.                 dp[i][j] += max(dp[i+1][j],dp[i+1][j+1]);  
      30.             }  
      31.         }  
      32.               
      33.         printf("%d\n",dp[0][0]);  
      34.     }  
      35.     return 0;  
      36. }  


      代码二:


      [cpp] view plain copy
      print?
      1. #include <cstdio>  
      2. #include <cmath>  
      3. #include <cstring>  
      4. #include <iostream>  
      5. #include <algorithm>  
      6. using namespace std;  
      7. #define maxn 1010  
      8. int dp[130][maxn];  
      9. int main()  
      10. {  
      11.     int t;  
      12.     int n, m;  
      13.     scanf("%d",&t);  
      14.     while(t--)  
      15.     {  
      16.         scanf("%d",&n);  
      17.         memset(dp,0,sizeof(dp));  
      18.         for(int i = 1; i <= n; i++)  
      19.         {  
      20.             for(int j = 1; j <= i; j++)  
      21.             {  
      22.                 scanf("%d",&dp[i][j]);  
      23.             }  
      24.         }  
      25.         for(int i = 1; i <= n; i++)  
      26.         {  
      27.             for(int j = 1; j <= i; j++)  
      28.             {  
      29.                 dp[i][j]+=max(dp[i-1][j],dp[i-1][j-1]);  
      30.             }  
      31.         }  
      32.         int maxx = 0;  
      33.         for(int i = 1; i <= n; i++)  
      34.         {  
      35.             maxx = max(maxx,dp[n][i]);  
      36.         }  
      37.         printf("%d\n",maxx);  
      38.     }  
      39.     return 0;  
      40. }  


      5
      0
       
       

      我的同类文章

      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
      原创粉丝点击