Digital triangle Problems

Source: Internet
Author: User

Digital triangle Problems

Memory limit: 65536kb time limit: 300 ms

Accept: 62 submit: 146

Description

Shows a digital triangle composed of N numbers. Design an algorithm to calculate a path from the top of the triangle to the bottom, so that the total number passing through the path is the largest.

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

For a given number triangle composed of N rows of numbers, calculate the maximum value of the number and the path from the top to bottom of the triangle.

Input

The first line of the input is the number of N, 1 <= n <= 600. The next n rows are numbers in each row of a digital triangle. All numbers are in the range of 0 .. 99.

Output

Output: the maximum value.

Sample Input

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

Sample output

#include<iostream>#include<stdio.h>#include<stdlib.h>#include<stdlib.h>usingnamespacestd ;int
Data[605][605];
int
M[605][605];
int
main()
{    intT = 0 ;    scanf("%d",&T) ;    intk = 1 ;    inti = 1 ;    intj = 1 ;    memset(M,0,sizeof(M));    while(k<=T)    {        for(j=1;j<=k;j++)        {            scanf("%d",&Data[k][j]);            //cout<<"\nData: i: "<<k<<" J:"<<j <<" "<<Data[k][j]<<" ";        }        k++;    }    for( i =T ; i >=1 ; i -- )        for(j =1 ; j <= i ; j++ )        {             if( i ==T )            {                M[i][j] = Data[i][j];            }            else{                inta = M[i+1][j] +Data[i][j];                intb = M[i+1][j+1] +Data[i][j];                a>b ? M[i][j] = a : M[i][j] = b ;            }        //  cout<<"\nM: i: "<<i<<" J:"<<j <<" "<<M[i][j]<<" ";        }         cout<<M[1][1]<<endl;    return0 ;}

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.