HDU 2084 Number Tower
Recursive from bottom to top, state transfer equation Dp[i][j] = max (Dp[i+1][j], dp[i+1][j+1]) + a[i][j];
/*HDU 2084 Tower---Getting Started DP*/#include<cstdio>Const intN = the;intDp[n][n];intMAX (intAintb) { returna > B?a:b;}intMain () {#ifdef _local freopen ("D:\\input.txt","R", stdin); #endif intT; scanf ("%d", &t); while(t--){ intN; scanf ("%d", &N); for(inti =1; I <= N; ++i) { for(intj =1; J <= I; ++j) {scanf ("%d", &Dp[i][j]); } } ////dp[i][j] Record the maximum path from the bottom up to the i,j, the final result and the dp[1][1] is the longest path //for (int i = 1; I <= n; ++i) {// //The DP of the last line is initialized to the value of a[i][j] itself; //Dp[n][i] = a[n][i]; //} //state transition equation: dp[i][j] = max (D[i+1][j] + dp[i+1][j+1]) + a[i][j]//That is, the maximum number of two adjacency points to the next line plus the value of that point is the maximum value for that point. for(inti = n-1; i >0; --i) { //line I has the number of I, from the bottom to go up, for(intj =1; J <= I; ++j) {Dp[i][j]= MAX (Dp[i +1][J], Dp[i +1][j +1]) +Dp[i][j]; //use Dp[i][j directly] to omit the value of an array//Dp[i][j] = MAX (Dp[i + 1][j], dp[i + 1][j + 1]) + a[i][j]; } } //recursive from the bottom to eliminate the last cycle to find the maximum value and go directly to Dp[1][1]printf"%d\n", dp[1][1]); } return 0;}
View Code
HDU 2084 Tower---Getting Started DP