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

思路:这是一道比较简单的动态规划题,有两种方法,其实相差不多都是递推求的。

第一种:从上到下推,用dp数组来记录,dp[i+1][j]=max(dp[i+1][j],dp[i][j]+m[i+1][j]);

            dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+m[i+1][j+1]);最后将最后一行比较找出最大值即可。

#include<cstdio>
#include <iostream>
using namespace std;
const int maxn=355;
int m[maxn][maxn],dp[maxn][maxn];
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;++i)
for(int j=0;j<=i;++j)
scanf("%d",&m[i][j]);
dp[0][0]=m[0][0];
for(int i=0;i<n-1;++i)
for(int j=0;j<=i;++j)
{
dp[i+1][j]=max(dp[i+1][j],dp[i][j]+m[i+1][j]);
dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+m[i+1][j+1]);
}
int sum=dp[n-1][0];
for(int i=0;i<n;++i)
sum=max(sum,dp[n-1][i]);
printf("%d\n",sum);
return 0;
}

第二种:从下往上推,每个位置都从下边两个位置中选一个大的相加,这样一直往上,直到m[0][0],就求出来了。

#include<cstdio>
#include <iostream>
using namespace std;
const int maxn=355;
int m[maxn][maxn];
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;++i)
for(int j=0;j<=i;++j)
scanf("%d",&m[i][j]);
for(int i=n-2;i>=0;--i)
for(int j=0;j<=i;++j)
m[i][j]+=max(m[i+1][j],m[i+1][j+1]);
printf("%d\n",m[0][0]);
return 0;
}

poj3176-Cow Bowling【dp】的更多相关文章

  1. USACO Cow Pedigrees 【Dp】

    一道经典Dp. 定义dp[i][j] 表示由i个节点,j 层高度的累计方法数 状态转移方程为: 用i个点组成深度最多为j的二叉树的方法树等于组成左子树的方法数 乘于组成右子树的方法数再累计. & ...

  2. Kattis - honey【DP】

    Kattis - honey[DP] 题意 有一只蜜蜂,在它的蜂房当中,蜂房是正六边形的,然后它要出去,但是它只能走N步,第N步的时候要回到起点,给出N, 求方案总数 思路 用DP 因为N == 14 ...

  3. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  4. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  5. HDOJ 1257 最少拦截系统 【DP】

    HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  6. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  7. HDOJ_1087_Super Jumping! Jumping! Jumping! 【DP】

    HDOJ_1087_Super Jumping! Jumping! Jumping! [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  8. POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】

    POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...

  9. HackerRank - common-child【DP】

    HackerRank - common-child[DP] 题意 给出两串长度相等的字符串,找出他们的最长公共子序列e 思路 字符串版的LCS AC代码 #include <iostream&g ...

随机推荐

  1. node.js中的require

    初初接触node.js,印象最深刻的就是开头密密麻麻的require了. 这是什么东西? 其实也没啥大惊小怪的.require就是为了引用别的js文件,利于模块化编程,重用.以及避免过多代码挤在同一个 ...

  2. FourCC

    https://en.wikipedia.org/wiki/FourCC A FourCC (literally, four-character code) is a sequence of four ...

  3. c++ 编译器会绕过拷贝构造函数

    C++ primer P442 P447:在拷贝初始化过程中,编译器可以跳过拷贝构造函数,直接创建对象.即,编译器允许将下面的代码 "; //1 改写为 "); //2 由于str ...

  4. C - Gr-idian MST

    Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem Statement On an xy plane, in an ...

  5. bzoj2253

    cdq分治+dp 看见三维偏序是cdq,互相包含是最长上升子序列 这个代码是错的 交了两份代码,发现手动出数据是不一样的... 不调了 #include<bits/stdc++.h> us ...

  6. Tyvj1305最大子序和(单调队列优化dp)

    描述 输入一个长度为n的整数序列,从中找出一段不超过M的连续子序列,使得整个序列的和最大. 例如 1,-3,5,1,-2,3 当m=4时,S=5+1-2+3=7当m=2或m=3时,S=5+1=6 输入 ...

  7. Dex文件方法数超过65536怎么破?

    你的应用中的Dex 文件方法数超过了最大值65536的上限,会提示你: UNEXPECTED TOP-LEVEL EXCEPTION:java.lang.IllegalArgumentExceptio ...

  8. Codeforces 769C

    很久没有发题解,今天这题卡了下百度没看到相关题解,最后还是看了官方题解才找到原本思路的bug过的. 题意:给出一个二维迷宫,*表示墙,. 表示路,X表示起点,问一个长度为k的路径,从X出发并且回到X, ...

  9. HTML+CSS(12)

    n  CSS浮动和清除 Float:让元素浮动,取值:left(左浮动).right(右浮动). Clear:清除浮动,取值:left(清除左浮动).right(清除右浮动).both(同时清除上面的 ...

  10. Android 控制硬加速 hardwareAccelerated

    从Android3.0 (API level11)开始,Android的2D显示管道被被设计得更加支持硬加速了.硬加速使用GPU承担了所有在View的canvas上执行的绘制操作. 启用硬加速最简单的 ...