hdu 2084 the number of towers from simple recursive down

DP algorithm telling the time, a classic example is the number of towers issue, it is described:

There follows a number of towers required to come to the bottom from the top, if the numbers can only go every step of the adjacent nodes, the nodes through which the sum is the largest number?
Here Insert Picture Description
Told you, this is a subject DP, AC can you do?
The Input first input data comprising an integer number of C, showing an example of the test, the first line of each test case is an integer N (1 <= N < = 100), the number represents the height of the column, the next column by N row represents a number, where the i-th row have integer i, and are all integers in the interval [0,99] within.

Output per line for each test case and maximum output, the output may be obtained for each instance.

Sample Input1
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Sample Output
30

Recurrence relations dis [i] [j] = a [i] [j] + max (dis [i + 1] [j], dis [i + 1] [j + 1])

#include<bits/stdc++.h>


#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>

using namespace std;

int a[110][110];
int dis[110][110];

int maxx(int a,int b)
{
    return a>b?a:b;
}
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        memset(dis,0,sizeof(dis));
        int i,j;
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=i; j++)
            {
            scanf("%d",&a[i][j]);
            }
            if(i==n)
                dis[i][j] = a[i][j];
        }
        for(i=n; i>=1; i--)
        {
            for(j=1; j<=i; j++)
            {
                dis[i][j] = maxx(a[i][j]+dis[i+1][j],a[i][j]+dis[i+1][j+1]);
            }
        }
        printf("%d\n",dis[1][1]);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44694282/article/details/89737963