Digital triangle Algorithm

Source: Internet
Author: User

Description
A digital triangle is displayed. Compile a program to calculate a path from top to bottom


Path to maximize the total number of the path.Each step can be down or down along the left slash

Line down;1 <Number of triangle rows <25; number in triangle is an integer <1000;

Input

The first action N indicates that N rows are followed by N rows, indicating the path right of each triangle.

Output

The greatest answer to the sum of the numbers passed by the road Sutra.

Sample input 5
73 88 1 02 7 4 44 5 2 6 5 sample output 30

Easy to make mistakes: First, you need to understand the meaning of the question. The question is from left down or from right down; the actual figure is like this.

73 88 1 02 7 4 44 5 2 6 5 the 8 in the second row cannot be added to the 8 in the third row. This is important

The idea is to first read the number of the triangle in a two-dimensional array, and then open a two-dimensional array to store the maximum value from top to bottom through the point. Each point has only two conditions, first, it is transmitted from the top, and from the front of the top. Therefore, you only need to compare the two sizes and then give the value of a large number to the value of the array at the current position. Of course, except the first and last rows. You can assign values to the first and last rows ., For example, in the third row, when the value reaches 1, the value may be greater than the value 3 on the left, or the value 8 on the top may be greater than the value 1. Here, you only need to compare the value, then assign values.

For details, see the code.

# Include <iostream>

Using namespace std;
Int main ()
{
Int a [100] [100], B [100] [100];
Int I, j, N;
Cin> N;
For (I = 0; I <N; I ++)
For (j = 0; j <= I; j ++) // receives the number of a digital triangle.
Cin> a [I] [j];
B [0] [0] = a [0] [0];
For (I = 1; I <N; I ++)
{
For (j = 0; j <= I; j ++)
{
If (j = 0)
B [I] [j] = B [I-1] [j] + a [I] [j]; // when j is 0, it is the addition of the first line
Else if (I = j)
B [I] [j] = B [I-1] [J-1] + a [I] [j]; // when I = j, It is the addition of the last oblique line;
Else
{
Int x = B [I-1] [J-1] + a [I] [j];
Int y = B [I-1] [j] + a [I] [j];
B [I] [j] = x> y? X: y;
}
}
}
Int max = B [N-1] [0];
For (I = 0; I <N; I ++)
If (max <B [N-1] [I])
Max = B [N-1] [I];
Cout <max <endl;
}

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.