zoukankan      html  css  js  c++  java
  • hdu 2084 数塔

    数塔

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

    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
     1 #include <iostream>
     2 using namespace std;
     3 int reward[100][100], best[100][100];
     4 int main() {
     5     int test, n;
     6     cin >> test;
     7     
     8     while(test--){
     9         cin >> n;
    10 
    11         for(int i = 0; i < n; i++)
    12             for(int j = 0; j <= i; j++)
    13                 cin >> reward[i][j];
    14 
    15         best[0][0] = reward[0][0];
    16 
    17         for (int i = 1; i < n; i++){
    18             for (int j = 0; j <= i; j++) {
    19                 if(j == 0)
    20                     best[i][j] = best[i-1][j] + reward[i][j];
    21                 else if (i == j)
    22                     best[i][j] = best[i-1][j-1] + reward[i][j];
    23                 else
    24                     best[i][j] = (best[i-1][j] > best[i-1][j-1] ? best[i-1][j] : best[i-1][j-1]) + reward[i][j];
    25             }
    26         }
    27 
    28         int max = best[n-1][0];
    29         for(int i = 1; i < n; i++){
    30             if(best[n-1][i] > max)
    31                 max = best[n-1][i];
    32         }
    33 
    34         cout << max << endl;
    35     }
    36     //system("pause");
    37     return 0;
    38 }
    越努力,越幸运
  • 相关阅读:
    pcb过孔盖油
    stm32的串口中断
    串口速度计算
    块元素、行内元素、行内块元素及其相互转化
    CSS伪类选择器
    CSS后代选择器、子代选择器
    CSS表示颜色、行间距、缩进、文字修饰
    CSS学习之通配符选择器
    【DP专题】——洛谷P0170道路游戏
    ssh框架jar包下载地址
  • 原文地址:https://www.cnblogs.com/qinduanyinghua/p/5483266.html
Copyright © 2011-2022 走看看