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=-1;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=i;j++)
                cin>>a[i][j];
        }
        dp[1][1]=a[1][1];
        for(int i=2;i<=n;i++){
            for(int j=1;j<=i;j++)
            {
                dp[i][j]=max(dp[i-1][j-1],dp[i-1][j])+a[i][j];
                maxx=max(dp[i][j],maxx);
            }
        }

        cout<<maxx<<endl;
    }
    return 0;
}
时间: 2024-12-15 15:02:01

Foj 1004 Number Triangle[ 数塔 ]的相关文章

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 573 88 1 02 7 4 44 5 2 6 5 212 3 Sample Output 304 DP水题 记录一下 自下往上走 左右始终选择较大的与之上的相加 #include<iostream> #include<algorithm> #include

POJ 3176(Cow Bowling )(就是简单的数塔,动态规划)

Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14206   Accepted: 9428 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

dp入门--poj 1163数塔

The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36138   Accepted: 21615 Description 73 88 1 02 7 4 44 5 2 6 5(Figure 1) Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a

POJ 3176 Cow Bowling 保龄球 数塔问题 DP

题目链接:POJ 3176 Cow Bowling Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14044   Accepted: 9310 Description The cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though,

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

Leetcode:Triangle 三角形塔最小路径和

Triangle: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to botto

HDU 1619 &amp; UVA 116 Unidirectional TSP(树形dp,入门 , 数塔变形)

Unidirectional TSP Description Background Problems that require minimum paths through some domain appear in many different areas of computer science. For example, one of the constraints in VLSI routing problems is minimizing wire length. The Travelin

数塔 Easy

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

1002 数塔取数问题

1002 数塔取数问题 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 一个高度为N的由正整数组成的三角形,从上走到下,求经过的数字和的最大值. 每次只能走到下一层相邻的数上,例如从第3层的6向下走,只能走到第4层的2或9上. 5 8 4 3 6 9 7 2 9 5 例子中的最优方案是:5 + 8 + 6 + 9 = 28 Input 第1行:N,N为数塔的高度.(2 <= N <= 500) 第2 - N + 1行:每行包括1层数塔的数字,第2行1个数,第3