POJ 1163 the Triangle

Source: Internet
Author: User

Description

Figure 1 shows a number triangle. Write A program this calculates the highest sum of numbers passed on a route this starts at the top and ends somewhere on The base. Each of the step can go either diagonally down to the left or diagonally down to the right.

Input

Your program was to read from standard input. The first line contains one integer n:the number of rows in the triangle. The following N lines describe the data of the triangle. The number of rows in the triangle are > 1 but <= 100. The numbers in the triangle, all integers, is between 0 and 99.

Output

Your program is-to-write to standard output. The highest sum is a written as an integer.

Sample Input

573 88 1 0 2 7 4 44 5 2 6 5

Sample Output

30

//Do the first DP question, come on

The idea is to create an array that is dynamically planned from the bottom up , saving the maximum value of the page child node to the current node

#include <iostream>#include<cmath>#include<algorithm>#include<cstdio>using namespacestd;intMain () {intdp[102][102]; intN;  while(cin>>N) { for(intI=1; i<=n;i++)          for(intj=0; j<i;j++) Cin>>Dp[i][j];  for(inti=n-1; i>=1; i--)          for(intj=0; j<i;j++) Dp[i][j]+ = max (dp[i+1][j],dp[i+1][j+1]); cout<<dp[1][0]<<Endl; }    return 0;}

POJ 1163 the 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.