See the article on https://dyingdown.github.io/2019/12/20/HDU-2084%20Number-Tower/
Number Tower
When describing the DP algorithm, a classic example is the number tower problem, which is described as follows:
There are counting towers as shown below. It is required to walk from the top to the bottom. If each step can only go to adjacent nodes, what is the maximum number of nodes that pass by?

I have already told you that this is a DP topic. Can you AC?
Input
The input data first includes an integer C, which represents 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 number tower, and is then represented by N rows of numbers. Number tower, where there are i integers in the i-th row, and all integers are in the interval [0,99].
Output
For each test instance, the maximum possible output is obtained, and the output for each instance is on one line.
Sample Input
1
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Sample Output
30
Analysis
This is an very easy task. As you can easily see, for each point t o w e r [ i ] [ j ] tower[i][j] tower[i][j] , it has two parent points, t o w e r [ i − 1 ] [ j − 1 ] tower[i - 1][j - 1] tower[i−1][j−1] and t o w e r [ i − 1 ] [ j ] tower[i - 1][j] tower[i−1][j]. The longest path from the root point to the current points is directly base on his parent point.
So you store the length of the path from the first point to the current point in the current point’s position. So how to choose the longest path?
Since it’s parents status has changed into the total length, this point’s status should be the biggest parent of the two plus this points original status.
Code
#include<bits/stdc++.h>
using namespace std;
int tower[200][200];
int main() {
memset(tower, 0, sizeof(tower));
int t, n;
cin >> t;
while(t --) {
cin >> n;
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= i; j ++) {
cin >> tower[i][j];
}
}
for(int i = 1; i <= n; i ++) {
for(int j = 1;j <= i; j ++) {
tower[i][j] += max(tower[i - 1][j - 1], tower[i - 1][j]);
}
}
int maxn = 0;
for(int i = 1; i <= n; i ++) {
maxn = max(maxn, tower[n][i]);
}
cout << maxn << endl;
}
return 0;
}
本文介绍了一个经典的动态规划问题——数塔问题。通过分析数塔的结构,我们发现每个节点的状态取决于其父节点状态的最大值。文章提供了一个C++代码示例,展示了如何使用动态规划求解数塔问题,以找到从顶部到底部经过节点数量最多的路径。
5389

被折叠的 条评论
为什么被折叠?



