Number of towersTime
limit:1000MS
Memory Limit:32768KB
64bit IO Format:%i64d &%i64 U SubmitStatusPracticeHDU 2084
Description
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
1573 88 1 0 2 7 4 44 5 2 6 5
Sample Output
30
Idea: From the bottom up, each position of the DP element is the maximum value of this vertex, the first line of the DP value is equal to itself, and then each rise one line to fill out the top element of the DP value. (Interesting is the C + + compiled after tle, instead of g++ to become the fastest solution, laugh and cry)
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4 #defineMAX 1055 6 intMainvoid)7 {8 intC,n;9 intDp[max][max];Ten Onescanf"%d",&c); A while(C--) - { -scanf"%d",&n); the for(inti =1; I <= N;i + +) - for(intj =1; J <= I;j + +) -scanf"%d",&dp[i][j]); - + for(inti = n-1; I >=1; I--) - for(intj =1; J <= I;j + +) +DP[I][J] + = Dp[i +1][J] > Dp[i +1][j +1] ? Dp[i +1][J]: Dp[i +1][j +1]; Aprintf"%d\n", dp[1][1]); at } - - return 0; -}
HDU 2084 number Tower (DP)