Dynamic Programming _ Digital triangles

Source: Internet
Author: User

Problem Description: Look for a path from the top to the bottom in the number triangle below, so that the sum of the numbers passing through the path is the largest, and each step on the path can only go down to the left or right. Just ask for this maximum and can, do not have to give a specific path.

Requirements for entering data: The number of rows in the triangle is greater than 1 and is less than or equal to 100, and the number is 0--99.

Input format:

5 //Triangle line number, below is the triangle

7

3 8

8 1 0

2 7 4 4

4 5 2) 6 5

#include <iostream>using namespace Std;const int N = 101;int A[n][n], N;int main () {cin >> n;for (int i = 0; i& Lt;n; i++) {for (int j = 0; j<=i; j + +) {cin >> a[i][j];}} for (int i = n-2; i>=0; i--) {for (int j = 0; j<=i; j + +) {int x = a[i+1][j];if (x < a[i+1][j+1]) x = A[i+1][j+1];a[i ][J] = A[i][j] + x;}} cout << a[0][0]<< Endl;return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Dynamic Programming _ Digital triangles

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.