HDU 2084 Tower (DP preliminary application)

Source: Internet
Author: User

HDU 2084 Number Tower


When it comes to the DP algorithm, a classic example is the problem of the tower, which is described in this way:

As shown in the following tower, required to go from the top floor to the bottom, if each step can only go to adjacent nodes, then the number of nodes through the sum of the maximum is how much?

Already told you, this is a DP problem, can you AC?

Input

The input data first consists of an integer c, which indicates the number of test instances, the first line of each test instance is an integer N (1 <= N <= 100), which represents the height of the tower, followed by the number of columns in n rows, where line I has an integer i, and all integers are within the interval [0,99].

Output

For each test instance, the output may be the largest and one row per instance output.

Sample Input

Sample Output


Title: Chinese topic.

Problem Solving: First record the last line of the DP value, and then start from the last line to the upper layer progression.


#include <stdio.h> #include <string.h> #include <algorithm>using 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;}


HDU 2084 Tower (DP preliminary application)

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.