Getting DP | 1: Digital Triangles

Digital Triangles

Achievement 10 opening time 2020 March 24 Tuesday, 23:15
discount 0.8 Discount Time 2020 April 21 Tuesday, 23:55
Allow late no Closing time 2020 April 21 Tuesday, 23:55

Description of the problem: Given a triangle digital numbers n rows design a test algorithm, calculation of a path from top to bottom of the triangle, so that the path passes through a maximum and the number.

Algorithm Design: triangle for a given n-th row of numbers, and computes the maximum number of paths from top to bottom of the triangle pass.

Data Input: The first row of numbers triangular rows n, 1 <= n <= 100 the next n lines in each row is a digital triangular numbers all numbers between 0 to 99.

Output Results: The number of rows is the maximum value of the first calculated.

  Test input Expected output time limit Memory Limit Additional process
Test Case 1  
  1. 5↵
  2. 7↵
  3. 3 8↵
  4. 8 1 0 ↵
  5. 2 7 4 4↵
  6. 4 5 2 6 5↵
 
  1. 30↵
1 second 64M 0

It is well understood meaning of problems: In a digital triangle, in the end from the top end, and to select a path having a maximum element. (Only selectable elements each current position lower left / lower right) is an illustration of the FIG test case:

Simple and crude to think about: If using violence enumeration, from top to bottom a total of n-1 second choice for each case selected for the two kinds, so a total of  2^{n-1} paths. Eleven calculated and then select the maximum time complexity  O (2 ^ n) , OH, exponential time ┭┮﹏┭┮ bad.


1, analysis

This question is obviously a typical dynamic programming problem , because it meets the two characteristics of dynamic programming:

Ⅰ optimal substructure

From top to bottom of the optimum (and maximum) path, to any element to the bottom during a path it is optimal. Then we can push up from the bottom, the top draw optimal path. For example, the optimal path of the second layer is known, we find the optimal path of the top layer, the second layer is to select a path and more preferably 3 or 8 and 7 +. So, we can be seen as a sub-optimal path underlying layer of problems, the optimal path next layer is known, then the optimal path on a layer of easily available.

Ⅱ overlapping subproblems

As already analyzed, the question of a total of  2^{n-1} paths, and each path will have overlapping sub-problems. For example: 7-3-1-7-5 and 7-3-8-7-5 the reciprocal path of the second sub-layer to the underlying 7-5 overlap. This is why the use of dynamic programming instead of solving the root causes of divide and conquer method.


2, modeling

Data storage

The figure is a regular isosceles triangle, then we should be stored in the program memory using the corresponding two-dimensional array. The following figure shows a simple embodiment the branch portions should correspond stored in the array. We triangle each row corresponds to a one-dimensional array indices: i = 0..n-1, each column corresponding to a two-dimensional array indices: j = 0..i. In the digital triangle each selection path may be selected lower left / lower right, corresponds to the array, the elements a [i] to select [j] should be at a [i + 1] [j ] or a [i + 1 ] [+ J. 1] .

A Dynamic Programming

Optimal substructure has analyzed this question, the optimal solution can be introduced optimal solution underlying the top layer, the solution process is a bottom-up is. Using a two-dimensional array dp [i] [j] to store a [i] [j] to the bottom of the element values of the optimal path. So it is easy to list the state transition equation:

当 i > n-1 : dp[i][j] = a[i][j] + max(dp[i+1][j] , dp[i+1][j+1])

When i = n - 1 (Initial conditions): dp[i][j] = a[i][j] 


3, code implementation 

AC codes directly attached 

#include <cstdio>
#include <algorithm>
using namespace std;
#define MAXN 105

int main() {
    int n;
    scanf("%d", &n);
    int a[MAXN][MAXN], dp[MAXN][MAXN];

    /* 处理输入的数字三角形 */
    for (int i = 0; i < n; i++)
        for (int j = 0; j <= i; j++) {
            scanf("%d", &a[i][j]);
            dp[i][j] = a[i][j];  //初始化dp数组
        }

    /* 自底向上的求解过程 */
    for (int i = n - 2; i >= 0; i--)  //从 n-2 ~ 0 层
        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]);  //输出结果
}


Have any questions please review the exchange, if you wish this article helpful little bit of praise, hee hee ~  



end 

No personal welcome attention to the public "  chicken wings Programming" , here is a serious and well-behaved code agricultural one.

---- do the most well-behaved blog er, do the most solid programmer ----

Aims to carefully write each article, usually aggregated into notes will push updates -

Here Insert Picture Description

Published 138 original articles · won praise 63 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43787043/article/details/105106108