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. asp.net web api集成微信服务(使用Senparc微信SDK)- z

    /// <summary> /// 微信请求转发控制器 /// </summary> [RoutePrefix("weixin")] public clas ...

  2. C#winform中调用wpf

    原文:C#winform中调用wpf 在WinForm中是可以使用WPF中的控件(或者由WPF创建的自定义控件) 1.新建一个winform项目: 2.在解决方案上新建一个wpf项目: 如图: 如果有 ...

  3. C#基础巩固(2)-Linq To XML创建XML

    一.首先要清楚一个正确的XML基本格式是怎样的. 1.后缀名.xml结尾 2.有一行描述 3.有且仅有一个根节点. 如图: 一个正确的xml文件能够被浏览器打开且显示.所以判断一个xml文件有没有错误 ...

  4. Luogu P3388 【模板】割点(割顶)

    一道求割点的板子题.还是采用经典的Tarjan算法. 首先大致和Tarjan求强连通分量相似,都是用\(dfn_x\)表示访问到\(x\)的时间(时间戳),\(low_x\)表示通过\(x\)回边能走 ...

  5. VitualBox安装linux记录

    下载镜像 CentOS 7镜像下载 阿里云站点:http://mirrors.aliyun.com/centos/7/isos/x86_64/ VirtualBox安装linux https://ww ...

  6. WPF 录屏软件研发心得及思路分享(已结束开发)

    最近由于工程需要开始研发基于Windows的自动录屏软件,很多细节很多功能需要处理,毕竟一个完美的录屏软件不是你随随便便就可以写出来的.首先参考了大部分的录屏软件,在研发的过程中遇到了很多的问题:比如 ...

  7. Redis未授权访问漏洞的利用及防护

    Redis未授权访问漏洞的利用及防护 什么是Redis未授权访问漏洞? Redis在默认情况下,会绑定在0.0.0.0:6379.如果没有采取相关的安全策略,比如添加防火墙规则.避免其他非信任来源IP ...

  8. 软工个人博客-week7

    Part 1       No Silver Bullet - Essence and Accidents of Software Engineering软件工程中没用通用的方法或者技术让软件工程在短 ...

  9. 《Linux内核设计与实现》读书笔记 4 进程调度

    第四章进程调度 进程调度程序可看做在可运行太进程之间分配有限的处理器时间资源的内核子系统.调度程序是多任务操作系统的基础.通过调度程序的合理调度,系统资源才能最大限度地发挥作用,多进程才会有并发执行的 ...

  10. <<架构漫谈>>读后感

    今天按照老师的要求,看了架构漫谈1--9讲,觉得受益良多,以下是我得点点读后感: (一)什么是架构? 架构的英文是Architecture,从定义上看,架构好像是一个过程,也不是很清晰.下面从架构的缘 ...