Dynamic Programming--Digital triangle problem

Source: Internet
Author: User

1. Description of the problem

There is a number triangle like this:
         

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

Starting at the vertex, each number goes down the lower left and bottom two directions, finding the sum of the paths to the last row.

Input
The 1th line is the number of rows n,1<= n <=100 of the numeric triangle.

The next n rows are the numbers in each row of the number triangle. All numbers are between 0---99.

For example, input is:

5

7

3 8

8 1 0

2 7 4 4

4 5 2) 6 5

The output is 30.

2. Problem solving

This is a typical dynamic solver problem because it conforms to the nature of the problem that dynamic planning solves:

Optimal substructure Properties

Overlap of sub-problems

See this topic, it is easy to think of the topic of multi-paragraph diagram, in fact, the truth is the same, the only difference is: one is from some vertex to an end to find the minimum value; one is a starting point to some point to find the maximum value.

So both the state and the transfer equation can mimic the solution of a multi-segment graph:

\[f (i,j) = \max \{f (i-1,j-1), f (i-1,j) \} + {c_{ij}}\]

Where I represents the number of rows of the triangle where the point is located (the number of rows is calculated from 0), and J indicates the number of rows in which the point is located, that is, the column count (the number of columns starts from 0).

Note: The i,j in the transfer equation cannot be crossed when the code is calculated. So the F function of the numbers at each end of each line is calculated separately, and only one decision can reach them.

3. Code implementation

#define MAX 100#define Getmax (x, y) (x>y x:y) #include <stdio.h>void main (void) {freopen ("In.txt", "R", stdin); f Reopen ("OUT.txt", "w", stdout);//Initialize array elements all 0int Path[max][max] = {0};int Dist[max][max] = {0};//for triangle assignment int i = 0;int J = 0 ; int n = number of rows of 0;//triangles scanf ("%d", &n), for (i= 0; i < n; i++) {for (j = 0; J <= I; j + +) {scanf ("%d", &path[i][j]);} }//for//to prevent the transfer equation from crossing out, the beginning and end of each line is first calculated with the longest path dist[0][0] = path[0][0];for (i = 1; i < n; i++) {dist[i][0] = dist[i-1][0] + path[i][0 ];} Forfor (i = 1; i < n; i++) {dist[i][i] = Dist[i-1][i-1] + path[i][i];} Calculates the longest path for each node in the middle for (i = 2; i < n; i++) {for (j = 1; j < I; J + +) {Dist[i][j] = Getmax (Dist[i-1][j-1], dist[i-1][j]) + PA TH[I][J];}} Find the longest path int maxsum = 0;for (j = 0; J < N; j + +) {if (Maxsum < dist[n-1][j]) {maxsum = Dist[n-1][j];}} forprintf ("%d", maxsum); fclose (stdin); fclose (stdout); return;}

Test data:

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

Dynamic Programming--Digital triangle problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.