A simple dynamic programming---a dynamic digital triangle

Source: Internet
Author: User

A Dynamic Digital triangle
Difficulty level: B; run time limit: 1000ms; operating space limit: 51200KB; code length limit: 2000000B
Question Description

A number consists of a triangle with n rows and the number of I in line I. Starting with the first number, you can go down one block at a time, down to the left or right, until you reach the last line, adding up the number of passes along the way. How to walk to get the biggest and?
As an example:

For the sake of simplicity, enter the number of each line sequentially, and do not enter a space before the first number.

Input
The first line: N, which indicates that the triangle has n rows
Second to n+1: the data for each row of the number triangle, entered sequentially, is separated by a space between two numbers.
Output
A number that represents the largest and
Input example
4
1
3 2
4 10 1
4 3 2 20
Output example
24
1#include <iostream>2#include <algorithm>3 using namespacestd;4 #defineMAXN 10005 intN,I,J,A[MAXN][MAXN];6 intMain ()7 {8scanf"%d",&n);9      for(i=1; i<=n;i++)Ten          for(j=1; j<=i;j++) scanf ("%d",&a[i][j]); One      for(i=n-1; i>=1; i--) A          for(j=1; j<=i;j++) A[i][j]=max (a[i][j]+a[i+1][j],a[i][j]+a[i+1][j+1]); -printf"%d\n", a[1][1]); -     return 0; the}
View Code

A simple dynamic programming---a dynamic digital triangle

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.