Hdu2084 _ shuita [simple question]

Source: Internet
Author: User
Shuta

Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/others) total submission (s): 22488 accepted submission (s): 13555
Problem description
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
1
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
 
Sample output
30
 
Source

/1/15 ACM Program Design Final Examination

Good understanding

Idea: If from above consideration, each time there are two choices, N layer tower has 2 ^ (N-1) A solution,

The traversal side is unscientific. In another way, consider from the bottom up, each time compare the size of near two numbers, let

Add a large number, so that each selection is the best case, move up and accumulate layer by layer, and finally add

At the top, the result is optimal, that is, the maximum.

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int map[110][110],dp[110];int main(){    int C,N;    scanf("%d",&C);    while(C--)    {        scanf("%d",&N);        memset(map,0,sizeof(map));        memset(dp,0,sizeof(dp));        for(int i = 1; i <= N;i++)        {            for(int j = 1; j <= i; j++)            {                scanf("%d",&map[i][j]);            }        }        for(int i = N; i>=1; i--)        {            for(int j = 1; j <= i;j++)            {                if(i == N)                    dp[j] = map[i][j];                else                {                    dp[j] = max(dp[j],dp[j+1]) + map[i][j];                }            }        }        printf("%d\n",dp[1]);    }    return 0;}



Hdu2084 _ shuita [simple question]

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.