Algorithms Chapter III Hands report - Dynamic Programming

1. Practice topic

7-1 Digital triangle (30)
 

Given a digital triangle formed by the n-th row numbers as shown in FIG. Try to design an algorithm to calculate a path from top to bottom of the triangle (each step downward along the left diagonal down or right diagonal), so that the digital sum of the maximum path passes.

QQ screenshot 20170929023616.jpg

Input formats:

Row n + 1 is input:

Line 1 is the number of rows triangular n, 1 <= n <= 100.

The next row n is the number of triangles in each row numbers. All numbers between 0..99.

Output formats:

The maximum value of the output path.

Sample input:

Here we are given a set of inputs. E.g:

5 
7 
3 8 
8 1 0 
2 7 4 4
4 5 2 6 5 

Sample output:

Given here corresponding output. E.g:

30

2. Description of the problem

    A digital triangle n rows, each row having n digits, from the top to low, can select only a path from the lower left corner or lower right corner of the selected digital down has been considered in the end portion of the path through which the digital sum maximum.

    My code:

#include<iostream>
#include<cmath>
using namespace std;

int main(){
    int n;
    cin>>n;
    int a[101][101];
    int sum[101][101];
    for(int i=1;i<=n;i++){
        for(int j=1;j<=i;j++){
            cin>>a[i][j];
        }
    }
    for(int i=1;i<=n;i++){
        sum[n][i] = a[n][i];
    }
    for(int i=n;i>0;i--){
        for(int j=1;j<=i;j++){
            if(sum[i][j]>=sum[i][j+1]){
                sum[i-1][j] = a[i-1][j] + sum[i][j];
            }
            else{
                sum[i-1][j] = a[i-1][j] + sum[i][j+1];
            }
        }
    }
    cout<<sum[1][1];
}

3. Algorithm Description

    N first inputs, n represents the line number, followed by a two-dimensional array a [i] [j] stored digital input, digital line corresponding to the row of the array, the array corresponding to the column position where the digital line and then start sorting, first, copy the last row sum [n] on a new two-dimensional array [i] = [n] a [i], comparing the number of the last line of the first and second number, and a large number of line adding a number to the storage array and a sum of the same array position, and then comparing the second number and third number, and then large numbers on the second row of the digital sum stored in the sum array and so on until the first row and the digital sum, sum [1] [1] is the value and the maximum path.

4. The time and space complexity

    The time complexity is O (n ^ 2), two heavy cycle sorting used, the time complexity is O (n ^ 2).

    Space complexity: the variables are allocated to the main function space is constant, so the space complexity is O (1).

5. personal experience

    Before practice, dynamic programming algorithm for the problem is still very vague, I do not know how to begin to start to consider the issue, after experiencing algorithm practical lesson in the discussion of the program and end gradually teammates slowly to solve the problem, have a deeper dynamic programming understanding, but in the realization of the idea of ​​there was still a feeling powerless, but also to try to play their own multi-code and come up with ideas to solve, from recursion optimization problem to a more simple way, later also need to work harder, pay more attempts before It will get better.

Guess you like

Origin www.cnblogs.com/ydh52/p/11708533.html