Simple entry DP (a) digital triangle

Digital triangle

  

  This question of the meaning of the questions is very simple, down the road from a point on the most, went to find the bottom of the maximum score available (maximum and path).

  Obviously if we laid down from the direction of the answer, then it is difficult, it is necessary every path again and then compare which way the weighted sum and bigger and then find the answer. This method for finding no doubt a lot of trouble, so we are thinking about how to optimize on A Method for Solving we go to a point of maximum forget many times, but we still have to go again used, re-search, resulting in the time complexity is very high, so we can take a point can obtain the maximum value recorded, and then directly call when in use. In order to better calculate this value, we will be a big triangle into smaller triangles, to solve the problems of small triangles. On the bottom of the triangle 1276, if the point 6 to go, then the next available maximum value is 12, we can point to the value 6 marked 6 + 12 = 18 That is to take this point may be a value obtained, the same triangular 71314 may be so considered, because 13> 7 14 down so that point 13 is the maximum value that can be obtained will be marked as 14 + 14 = 13 27, 132 415 the same operation triangle: the maximum value of 15 down to the point marked available: 15 + 24 = 39 24 118 likewise triangular, marked as 32 eight. And after that, we continue to push up the floor, because each point of the penultimate layer have recorded the best result of the last layer, so we can ignore the last layer, and the layer 4 as a last continue just one operation, for the triangle 18 (formerly 6) 27 (formerly 14) consisting of 12 take a maximum score of 12 can be obtained at this point is 12 + 27 = 39.27 7 39 Similarly, the marked 7 7 + 39 = 46.39 32 26 Similarly, the marker 26 to 26 + 39 = 65. now we erase the layer, the third layer as the final layer, just continuing the operation. For 39 (formerly 12) 46 (formerly 7) 11 triangles to 11 is updated to 11 + 36 = 57. Triangle for 46 (formerly 7) 65 (formerly 26) 8 composed of eight is updated to 8 + 65 = 73 now only the two. The original 11 became 57 is updated, the update would be the original 8 73. 13 should be selected so that the last 73 to go, so the maximum value of the entire triangle 13 + 73 (formerly 8) = 86.

Code:

#include<iostream>
#include<cstdio>
using namespace std;
int a[101][101];
int n;
int main()
{
    int k=0;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=i;j++)
        {
            cin>>a[i][j];
        }
    }
    a[2][1]+=a[1][1];
    a[2][2]+=a[1][1];
    for(int i=3;i<=n;i++)
    {
        for(int j=1;j<=i;j++)
        {
            if(a[i-1][j]>a[i-1][j-1]) a[i][j]=a[i][j]+a[i-1][j];
            else a[i][j]+=a[i-1][j-1];
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(a[n][i]>=k) k=a[n][i];
    }
    cout<<k;
}

 

 

 

  This question is the problem of dynamic programming entry, simple but well reflects the basic idea of ​​dynamic programming. Just learning dynamic programming students can look at, understand This phased approach to solve the problem, in order to reduce the time complexity of the effect of extra steps are omitted.

Dynamic programming is to simplify a big problem for the small questions to quickly find the optimal solution algorithms, there are elements of state and two transfer equation, and is essential in the algorithm competition in all areas, is a branch of mathematics of operations research.

  thanks for reading.

 

  

Guess you like

Origin www.cnblogs.com/tianbowen/p/11296194.html