USACO training course Number Triangles 数塔 /// DP oj10122

题目大意:

...就是数塔

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

7+3+8+7+5=30

Sample Input

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

2
1
2 3

Sample Output

30
4


DP水题 记录一下

自下往上走 左右始终选择较大的与之上的相加

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cstring>
using namespace std;
int dp[1005][1005];
int main()
{
    int t;
    while(~scanf("%d",&t))
    {
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=t;i++)
            for(int j=1;j<=i;j++)
                scanf("%d",&dp[i][j]);
        for(int i=t-1;i>=1;i--)
            for(int j=1;j<=i;j++)
                dp[i][j]+=max(dp[i+1][j],dp[i+1][j+1]);
        printf("%d\n",dp[1][1]);
    }

    return 0;
}

原文地址:https://www.cnblogs.com/zquzjx/p/8893773.html

时间: 2025-01-01 21:20:48

USACO training course Number Triangles 数塔 /// DP oj10122的相关文章

HDU 2048 数塔(DP)

数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 20786    Accepted Submission(s): 12486 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少

hdu 2085 数塔 -- dp模板题

数塔 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少? 已经告诉你了,这是个DP的题目,你能AC吗? Input 输入数据首先包括一个整数C,表示测试实例的个数,每个测试实例的第一行是一个整数N(1 <= N <= 100),表示数塔的高度,接下来用N行数字表示数塔,其中第i行有个i个整数,且所有的整数均在区间[0,99]内. Outp

HDU2391 Filthy Rich【数塔DP】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2391 题目大意: 在一个N*M的矩阵中,不同的位置上有不同重量的黄金,从矩阵的左上角开始,只能向右 或是向下走,问:从左上角走到右下角最多能获得多少黄金. 思路: 简单的数塔DP,状态转移方程:dp[j] = max(dp[j-1],dp[j])+ Map[i][j]. AC代码: #include<iostream> #include<algorithm> #include<

POJ3176_Cow Bowling【数塔DP】

Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14253 Accepted: 9461 Description The cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a standard bowl

数塔DP

在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少? 已经告诉你了,这是个DP的题目,你能AC吗? Input 输入数据首先包括一个整数C,表示测试实例的个数,每个测试实例的第一行是一个整数N(1 <= N <= 100),表示数塔的高度,接下来用N行数字表示数塔,其中第i行有个i个整数,且所有的整数均在区间[0,99]内. Output 对于每个测试实例,输出可能得到的最大和,

数塔~~dp学习_1

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084 数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 32468    Accepted Submission(s): 19417 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的

HDU 2084 数塔 (DP)

数塔 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 2084 Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少? 已经告诉你了,这是个DP的题目,你能AC吗? Input 输入数据首先包括一个整数

USACO 1.5 Number Triangles

Number Triangles Consider the number triangle shown below. Write a program that calculates the highest sum of numbers that can be passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the

Foj 1004 Number Triangle[ 数塔 ]

题目:数塔问题,dp[i][j]到第i行第j个数的最大值: 代码: #include<iostream> #include<cstdio> #include<cstring> #define mem(a,b) memset(a,b,sizeof a) using namespace std; int dp[1010][1010]; int a[1010][1010]; int main() { int n; while(cin>>n) { int maxx=