Dynamic programming - the problem of counting towers

Topic requirements

Given a number tower, its storage form is a lower triangular matrix as shown below. In this number tower, starting from the top, you can choose to go down or right at each node until you reach the bottom. Please find a path that maximizes the sum of values ​​on the path.

Input example (number of towers):

9

12  15

10   6    8

2    18   9    5

19   7    10   4    16

Sample output (max path sum):

59

Question analysis

To explain dynamic programming in one sentence is " remember what you have done before ". If it is more accurate, it is actually " remember the answer you got before ".

For this problem, we can analyze from top to bottom and calculate from bottom to top.

That is to say, it is wrong for you to always select the largest one from above and use a greedy algorithm. Because we don't know that the next node at a certain node is larger or smaller.

So we can find a maximum path from bottom to top and keep updating the maximum value until the second level, we have selected the maximum path for a day.

Take the topic as an example:

The data of the fifth layer are 19 7 10 4 16

Starting from the fourth floor: If you choose 2 , on the fifth floor (19, 7), you must choose 19

                             Then the maximum path can only be  2—>19 =  21;

                            If you choose 18 , on the fifth floor (7, 10), you must choose 10

                             Then the maximum path can only be  18—>10 =  28;

                            If you choose 9 , on the fifth floor (10, 4), you must choose 10

                             Then the maximum path can only be  9—>10 =  19;

                            If you choose 5 , on the fifth floor (4, 16), you must choose 16

                              Then the maximum path can only be  5—>16 =  21;


Third layer: If you choose 10 , in the fourth layer (2, 18), you must choose 18 (28>21)

                             Then the maximum path can only be  10—>18—>10 =  38;

                                           If you choose 6 , on the fourth floor (18, 9), you must choose 18 (28>19)

                             Then the maximum path can only be  6—>18—>10 =  34;

                                    If you choose 8 , on the fourth level (9, 5), you must choose 5 (21>19)

                             Then the maximum path can only be  8—> 5—>16 =  29;

........

By analogy, until the second level, you will know how to choose from the first level. Adding the maximum path of the second layer is the maximum path required by the question.

Represented in a table:

59

50

49

38

34

29

21

28

19

21

19

7

10

4

16

Code implementation analysis

#include <iostream>
#include <algorithm>

using namespace std;


int data[50][50];//存储原始数据
int dp[50][50];//存储动态规划过程中的数据
int n;//塔的层数

void ShuT()
{
    //先将数塔最底层数据放入动态规划dp数组第五层
    for (int i = 0; i < n; ++i)
    {
        dp[n - 1][i] = data[n - 1][i];
    }  


    //临时存放向下向右两个值的,最大那个
    int temp_max;


    //开始往dp数组中存放每个小阶段的路径值
     
   for (int i = n - 1; i >= 0; --i)     //层数,从下往上进行
    {
        for (int j = 0; j <= i; ++j)    //不同层数值的位置
        {
            // 使用递推公式计算dp的值
            temp_max = max(dp[i + 1][j], dp[i + 1][j + 1]); //取向下,向右的最大值
            dp[i][j] = temp_max + data[i][j];              //dp数组存放当前路径的最大值
        }
    }

}


int main()
{
   
    cin>>n;
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j <= i; ++j)
        {
            cin >> data[i][j];
        }
    }

    ShuT();
     cout << "最大路径和:" << dp[0][0] <<endl;
}

If there are any shortcomings, I hope everyone can make suggestions and make progress together! ! !

Guess you like

Origin blog.csdn.net/weixin_59367964/article/details/127936967