Digital Triangle 17 First-in DP

Source: Internet
Author: User

Description
7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 A number triangle is given. There are many different paths from the top of the triangle to the bottom. For each path, add up the number of paths above to get one and your task is to find the largest and. Note: Each step on the path can only go from one number to the next, or to the number on the right or to the nearest left.
Input
The input data has multiple groups, each input line is an integer N (1 < N <= 100), giving the number of rows of the triangle. The following n lines give the number triangles. The number on the number triangle ranges between 0 and 100.
Output
Outputs the maximum and.
Sample Input
573 88 1 0 2 7 4 44 5 2 6 5312 31 1 1
Sample Output
305

1#include <iostream>2 using namespacestd;3 intMain ()4 {5     intdata[ $][ $];//Defining Arrays6     intn,tmp;7      while(cin>>N)8     {9n=n+1;//It's a question, and I don't know why.Ten          for(intI=0; i<n;i++) One         { A              for(intj=0; j<i;j++) -Cin>>Data[i][j]; -         } the          for(intI=0; i<n;i++) -         { -              for(intj=0; j<i;j++) -             { +                 if(j==0)//Judging boundary problems -data[i][j]=data[i-1][j]+Data[i][j]; +                 Else AData[i][j]=max (data[i-1][j],data[i-1][j-1]) +data[i][j];//results from top down at             } -         } -tmp=0; -          for(intj=0; j<n;j++)//find the maximum value -         if(tmp<data[n-1][j]) -tmp=data[n-1][j]; incout<<tmp<<Endl; -     } to     return 0; +}
View Code

The first DP problem, the idea of this problem is, from top to bottom to save up

Master of this question http://blog.csdn.net/lalor/article/details/6954923

Digital Triangle 17 First-in DP

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.