HDU 2084 data tower (Preliminary Application of DP)

Source: Internet
Author: User

HDU 2084 data tower (Preliminary Application of DP)
HDU 2084 data Tower


When talking about the DP algorithm, a classic example is the data tower problem, which is described as follows:

There is a number tower as shown below. It is required to go from the top layer to the bottom layer. If each step can only go to adjacent nodes, what is the maximum sum of the numbers of the nodes that pass through?

I already told you that this is a DP question. Can you AC it?

Input

The input data first includes an integer C, indicating the number of test instances. The first row of each test instance is an integer N (1 <= N <= 100 ), the height of the tower. Next, use N rows of numbers to represent the tower. Row I has an I integer, and All integers are within the range [0, 99.

Output

For each test instance, the output is the largest possible sum, and each instance occupies one row of output.

Sample Input

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

Sample Output

 30 


Question: Chinese question.

Solution: first record the DP value of the last line, and then go up from the last line.


#include
 
  #include
  
   #includeusing namespace std;int a[105][105], d[105][105];int main() {int n;scanf("%d\n", &n);while (n--) {memset(a, 0, sizeof(a));memset(d, 0, sizeof(d));int m;scanf("%d\n", &m);int t = m;int cnt = 1;while (t--) {for (int i = 1; i <= cnt; i++) {scanf("%d", &a[cnt][i]);}cnt++;}for (int i = 1; i <= m; i++) {d[m][i] = a[m][i];} for (int i = m - 1; i >= 1; i--) {for (int j = 1; j <= i; j++) {d[i][j] = a[i][j] + max(d[i + 1][j], d[i + 1][j + 1]);}}printf("%d\n", d[1][1]);}return 0;}
  
 


Related Article

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.