HDU-2084 Number Tower

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?

img

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[i1][j1] and t o w e r [ i − 1 ] [ j ] tower[i - 1][j] tower[i1][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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值