hdoj:2084

数塔

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 39043    Accepted Submission(s): 23246

Problem Description

在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的:

有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?

已经告诉你了,这是个DP的题目,你能AC吗?

Input

输入数据首先包括一个整数C,表示测试实例的个数,每个测试实例的第一行是一个整数N(1 <= N <= 100),表示数塔的高度,接下来用N行数字表示数塔,其中第i行有个i个整数,且所有的整数均在区间[0,99]内。

Output

对于每个测试实例,输出可能得到的最大和,每个实例的输出占一行。

Sample Input

1
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

Sample Output

30
#include <iostream>
#include <vector>

using namespace std;
#define limit 101
#define Max(a,b) ((a) > (b) ? (a) : (b))
long a[limit][limit];

long max(long l1, long l2)
{
    return l1 > l2 ? l1 : l2;
}
int main()
{
    int T,n;

    cin >> T;
    while (T--)
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= i; j++)
                cin >> a[i][j];
        }
        for (int i = n-1; i >=1; i--)
        {
            for (int j = 1; j <= n-1; j++)
            {
                a[i][j] += Max(a[i + 1][j], a[i + 1][j + 1]);
            }
        }
        cout << a[1][1] << endl;
    }

    return 0;
}
时间: 2024-10-23 19:18:57

hdoj:2084的相关文章

hdoj:2086

A1 = ? Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7535    Accepted Submission(s): 4675 Problem Description 有如下方程:Ai = (Ai-1 + Ai+1)/2 - Ci (i = 1, 2, 3, .... n).若给出A0, An+1, 和 C1, C2, .....

hdoj:2085

核反应堆 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15706    Accepted Submission(s): 7036 Problem Description 某核反应堆有两类事件发生:高能质点碰击核子时,质点被吸收,放出3个高能质点和1个低能质点:低能质点碰击核子时,质点被吸收,放出2个高能质点和1个低能质点.假定开始的时

hdoj:2046

#include <iostream> using namespace std; long long fib(int n) { if (n == 1) return 1; if (n == 2) return 2; long long f1 = 1; long long f2 = 2; while (n >= 3) { long long f3 = f1 + f2; f1 = f2; f2 = f3; n--; } return f2; } int main() { int n; whi

hdoj:2045

#include <iostream> using namespace std; long long a[51]; int main() { int n; a[1] = 3; a[2] = 6; a[3] = 6; for (int i = 4; i <= 50; i++) { a[i] = a[i - 1] + 2 * a[i - 2]; } while (cin >> n) { cout << a[n] << endl; } }

hdoj:2042

#include <iostream> using namespace std; int main() { int n,a; while (cin >> n) { while (n--) { cin >> a; long num = pow(2, a) + 2; cout << num << endl; } } } a(n) = 2*a(n-1) -2 a(n) -2 = 2*{a(n-1) -2} a(n) = 2^n + 2

hdoj:2040

#include <iostream> #include <vector> using namespace std; vector<long> yueShu(long a) { vector<long> vec; vec.push_back(1); for (int i = 2; i < a; i++) { if (a%i == 0) { vec.push_back(i); //cout << i << " "

hdoj:2043

#include <iostream> #include <string> using namespace std; bool judgeSize(string str) { int size = str.size(); if (size < 8 || size>16) return false; return true; } int isA(string str) { for (auto &c : str) { if (c >= 'A' &&am

hdoj:2027

#include <iostream> #include <string> #include <vector> using namespace std; int main() { int n; cin >> n; string t; getline(cin, t); while (n >= 1) { string s; int a = 0, e = 0, i = 0, o = 0,u=0; getline(cin, s); for (char &

hdoj:2070

Fibbonacci Number Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 22530    Accepted Submission(s): 10375 Problem Description Your objective for this question is to develop a program which will g