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.

题意:每次向下或者向右下走,求最大和
分析:正向:每步来源于上方或者左上方,dp[i][j]表示第i行第j列的最大值
dp[i][j]=max{dp[i-1][j],dp[i-1][j-1]}+a[i][j].

#include<stdio.h>
#include<algorithm>
using namespace std;
int n,a[][],ans[][],maxans;
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
for(int j=;j<=i;j++)
scanf("%d",&a[i][j]);
for(int i=;i<=n;i++){
for(int j=;j<=i;j++){
ans[i][j]=max(ans[i-][j],ans[i-][j-])+a[i][j];
maxans=max(ans[i][j],maxans);
}
}
printf("%d",maxans);
return ;
}
逆向:逆着从n-1行到第1行,每次比较下方和右下方的大小,大的加上去,最后输出a[1][1]即可。

#include<stdio.h>
#include<algorithm>
using namespace std;
int n,i,j,a[][];
int main(){
scanf("%d",&n);
for(i=;i<=n;i++)
for(j=;j<=i;j++)
scanf("%d",&a[i][j]);
for(i=n-;i>=;i--)
for(j=;j<=i;j++)
a[i][j]+=max(a[i+][j],a[i+][j+]);
printf("%d",a[][]);
return ;
}

【POJ 3176】Cow Bowling(DP)的更多相关文章

  1. 【POJ 3176】Cow Bowling

    题 Description The cows don't use actual bowling balls when they go bowling. They each take a number ...

  2. 【POJ - 3045】Cow Acrobats (贪心)

    Cow Acrobats Descriptions 农夫的N只牛(1<=n<=50,000)决定练习特技表演. 特技表演如下:站在对方的头顶上,形成一个垂直的高度. 每头牛都有重量(1 & ...

  3. 【poj 3167】Cow Patterns(字符串--KMP匹配+数据结构--树状数组)

    题意:给2个数字序列 a 和 b ,问按从小到达排序后,a中的哪些子串与b的名次匹配. a 的长度 N≤100,000,b的长度 M≤25,000,数字的大小 K≤25. 解法:[思考]1.X 暴力. ...

  4. BZOJ 2296【POJ Challenge】随机种子(构造)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2296 [题目大意] 给出一个数x,求一个10的16次以内的数使得其被x整除并且数字包含 ...

  5. 【bzoj 1190】梦幻岛宝珠(DP)

    这题是在01背包问题的基础上,扩充了重量,需要用时间换空间. 思路: 1.仔细看题,注意到重量wi为a*2^b(a<=10,b<=30),很容易想到要按 b 分开做背包的DP.接下来的重点 ...

  6. 【POJ 1273】Drainage Ditches(网络流)

    一直不明白为什么我的耗时几百毫秒,明明差不多的程序啊,我改来改去还是几百毫秒....一个小时后:明白了,原来把最大值0x3f(77)取0x3f3f3f3f就把时间缩短为16ms了.可是为什么原来那样没 ...

  7. 【POJ - 3984】迷宫问题(dfs)

    -->迷宫问题 Descriptions: 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 ...

  8. 【POJ - 3176】牛保龄球 (简单dp)

    牛保龄球 直接中文了 Descriptions 奶牛打保龄球时不使用实际的保龄球.它们各自取一个数字(在0..99范围内),然后排成一个标准的保龄球状三角形,如下所示: 7 3 8 8 1 0 2 7 ...

  9. 【POJ 3167】Cow Patterns (KMP+树状数组)

    Cow Patterns Description A particular subgroup of K (1 <= K <= 25,000) of Farmer John's cows l ...

随机推荐

  1. SQL2005中的事务与锁定(九)-(2)- 转载

    -------------------------------------------------------------------------- Author : HappyFlyStone -- ...

  2. Ionic2 播放mp3功能实现

    在开发app的过程中有需要播放mp3的功能,一直想实现,但苦于具体的困难一直未能实现,经过一段时间的资料查询和测试,最终摸索出来,现记录如下: 1.最重要的是安装第三方插件ionic-audio,开源 ...

  3. oracle-union all与order by联合使用

    今天写sql的时候,第一次使用union all+order by .是个比较简单的问题,但由于对数据库的不深入理解导致犯了愚蠢的错.浪费了很多时间 如题: 两个子查询union all 需要用ord ...

  4. python语言程序设计3

    1, 起码坚持每天  半小时吧... 2, 上面有常用的几个数据类型..整数类型和字符串类型还行,但是列表类型的话就很蛋疼了. 3, 字符串类型的显著特点是在两侧有单引号或者双引号.用单引号和双引号表 ...

  5. RetrieveFavicon 获取任何站点的 favicon

    原文发表于我的技术博客 开源了一个获取任何站点 favicon 的类库,供使用. 原文发表于我的技术博客 RetrieveFavicon Project GitHub Retrieve favicon ...

  6. PAT甲级题解-1066. Root of AVL Tree (25)-AVL树模板题

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6803291.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  7. Notes of Daily Scrum Meeting(12.25)

    今天在学姐的帮助下,我们终于把网络连接的部分连通了,这对我们是一个很大的鼓舞,也找到了前期 连不通的问题在哪里,这让我们重新有了进行下去的勇气和决心,我们会在最后这几天把前端和后端结合, 做出我们最后 ...

  8. 五子棋游戏SRS文档

        SRS技术文档,包括对SRS的解释说明.SRS描述规范.软件需求规格说明书(SRS,Software Requirement Specification)是为了软件开发系统而编写的,主要用来描 ...

  9. zookeeper安装(Linux)

    安装环境: Linux:centos6.4 Jdk:1.7以上版本 Zookeeper是java开发的可以运行在windows.linux环境.需要先安装jdk. 安装步骤: 第一步:安装jdk 第二 ...

  10. Aspose for Maven 使用

    https://blog.aspose.com/2014/08/12/aspose-for-maven-aspose-cloud-maven-repository/ https://marketpla ...