HDU 2084 Number Tower
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
Sample Output
Title: Chinese topic.
Problem Solving: First record the last line of the DP value, and then start from the last line to the upper layer progression.
#include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;int a[105][105], d[105 ][105];int Main () {int n;scanf ("%d\n", &n), while (n--) {memset (A, 0, sizeof (a)), memset (d, 0, sizeof (d)), int m;scanf (" %d\n ", &m); int t = m;int cnt = 1;while (t--) {for (int i = 1; I <= cnt; i++) {scanf ("%d ", &a[cnt][i]);} cnt++;} for (int i = 1; I <= m; i++) {d[m][i] = A[m][i],} for (int i = m-1, I >= 1; i--) {for (int j = 1; J <= I; j + +) {D[i][j] = A[i][j] + max (d[i + 1][j], d[i + 1][j + 1]);}} printf ("%d\n", D[1][1]);} return 0;}
HDU 2084 Tower (DP preliminary application)