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 bowling-pin-like triangle like this:

7

3   8

8   1   0

2   7   4   4

4   5   2   6   5

Then the other cows traverse the triangle starting from its tip and moving "down" to one of the two diagonally adjacent cows until the "bottom" row is reached. The cow‘s score is the sum of the numbers of the cows visited along the way. The cow with the highest
score wins that frame.

Given a triangle with N (1 <= N <= 350) rows, determine the highest possible sum achievable.

Input

Line 1: A single integer, N

Lines 2..N+1: Line i+1 contains i space-separated integers that represent row i of the triangle.

Output

Line 1: The largest sum achievable using the traversal rules

Sample Input

5

7

3 8

8 1 0

2 7 4 4

4 5 2 6 5

Sample Output

30

Hint

Explanation of the sample:

7

*

3   8

*

8   1   0

*

2   7   4   4

*

4   5   2   6   5

The highest score is achievable by traversing the cows as shown above.

Source

USACO 2005 December Bronze

题目大意:给你一个三角形的数塔,问从上走到最下边,得到最大的和是多少

思路:从下往上推,当前值大的和上边的值相加

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[400][400],map[400][400];
int main()
{
    int N;
    while(~scanf("%d",&N))
    {
        memset(map,0,sizeof(map));
        for(int i = 1; i <= N; i++)
        {
            for(int j = 1; j <= i; j++)
            {
                scanf("%d",&map[i][j]);
            }
        }
        memset(dp,0,sizeof(dp));
        for(int i = N; i >= 1; i--)
        {

            for(int j = 1; j <= i; j++)
            {
                if(i == N)
                    dp[i][j] = map[i][j];
                else
                    dp[i][j] = max(dp[i+1][j],dp[i+1][j+1]) + map[i][j];
            }
        }
        printf("%d\n",dp[1][1]);
    }
    return 0;
}
时间: 2025-01-12 04:05:18

POJ3176_Cow Bowling【数塔DP】的相关文章

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<

数塔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 输入数据首先包括一个整数

hdu 2084 数塔 dp 动态规划

开始动态规划的学习了,先是比较基础的,很金典的数塔.附上题目链接  http://acm.hdu.edu.cn/showproblem.php?pid=2084 这题的状态转移方程是  dp[i][j] = max(dp[i-1][j-1],dp[i-1][j]) + m[i][j]; (dp[i][j] 表示在第 i 层 第 j 列时的最大和) . 然后一个双重循环,边能算出.当然可以用滚动数组.但是注意用滚动数组解题时,第二层循环 j 必须从大到小, 因为 状态转移方程 为  f  [ j

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

hdu2084 数塔 DP

数字三角形,DP裸题 1 #include<stdio.h> 2 #include<string.h> 3 #define max(a,b) (a)>(b)?a:b 4 5 int g[101][101],dp[101][101]; 6 7 int max1(int i,int j){ 8 return i>j?i:j; 9 } 10 11 int main(){ 12 int C; 13 while(scanf("%d",&C)!=EOF)