Hdu2084 data Tower

Source: Internet
Author: User
Shuta

Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 15753 accepted submission (s): 9399

When Problem description describes 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 data first includes an integer c, indicating the number of test instances. The first line 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 may be the largest sum, and the output of each instance occupies one row.

Sample Input

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

Sample output

30
 

Source2006/1/15 ACM Program Design Final Examination

Recommendlcy


Solution: this topic is the most basic dynamic planning question. First, if the processed data is in the (I, j) vertex (where I, j is in the Number Column), the maximum value of the current vertex is treee (I, j) + max (tree (I-1, J), tree (I-1, J-1), which is applicable to any position of the number Tower except the underlying layer. During processing, the formula can be used directly from the underlying layer to the top layer, and the data in the top layer data storage memory can be output.

# Include <cstdio> # include <algorithm> using namespace STD; int main () {int tree [101] [101]; int N, T; int I, J; scanf ("% d", & T); While (t --) {scanf ("% d", & N); for (I = 0; I <N; I ++) for (j = 0; j <= I; j ++) scanf ("% d", & tree [I] [J]); for (I = n-2; I> = 0; I --) for (j = 0; j <= I; j ++) tree [I] [J] = max (tree [I + 1] [J], tree [I + 1] [J + 1]) + tree [I] [J]; // you can directly set the state equation to printf ("% d \ n", tree [0] [0]);} return 0 ;}

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.