Several towers
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 54035 Accepted Submission(s): 31715
Problem Description
When talking about the DP algorithm, a classic example is the number tower problem, which is described
as follows: There is a number tower as shown below, which requires going from the top to the bottom. If each step can only go to the adjacent node, What is the maximum sum of numbers of nodes passed through?
I already told you, this is a DP question, can you AC?
as follows: There is a number tower as shown below, which requires going from the top to the bottom. If each step can only go to the adjacent node, What is the maximum sum of numbers of nodes passed through?
I already told you, this is a DP question, can you AC?
Input
The 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), indicating the height of the tower, followed by N lines of numbers. A number tower, where the i-th row has i integers, and all integers are in the interval [0,99].
Output
For each test instance, output the maximum possible sum, one line per instance.
Sample Input
1
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Sample Output
30
The bottom-up idea of the number tower is also very important in dp. Of course, this question can also be top-down, but it is necessary to traverse the maximum value in dp[ n ][ i ]
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int dp[105][105]; intmain() { int t; scanf("%d",&t); while(t--) { int n; memset(dp,0,sizeof(dp)); scanf("%d",&n); for(int i=1;i<=n;i++) { for(int j=1;j<=i;j++) { scanf("%d",&dp[i][j]); } } for(int i=n;i>=1;i--) { for(int j=1;j<=i;j++) { dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+dp[i][j]; } } printf("%d\n",dp[1][1]); } return 0; }