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.

 
渐渐对动态规划有所感悟 加油!
 #include <iostream>
#include <algorithm>
using namespace std;
int a[][];
int n;
int dp[][];
int main()
{
while(cin>>n){
for(int i=;i<n;i++){
for(int j=;j<=i;j++){
cin>>a[i][j];
}
}
int res=;
dp[][]=a[][];
for(int i=;i<n;i++){
for(int j=;j<=i;j++){
if(j==) dp[i][j]=dp[i-][j]+a[i][j];//第一列只有一个方向累加
else dp[i][j]=max(dp[i-][j],dp[i-][j-])+a[i][j];
res=max(res,dp[i][j]);
}
}
cout<<res<<endl;
}
return ;
}

另一种写法:倒推,感觉更清晰一点

 #include <iostream>
#include <algorithm>
using namespace std;
int n;
int a[][];
int dp[][];
int main()
{
while(cin>>n){
for(int i=;i<n;i++){
for(int j=;j<=i;j++){
cin>>a[i][j];
dp[i][j]=a[i][j];
}
}
for(int i=n-;i>=;i--){
for(int j=;j<=i;j++){
dp[i][j]+=max(dp[i+][j+],dp[i+][j]);
}
}
cout<<dp[][]<<endl;
}
return ;
}

Poj3176 Cow Bowling (动态规划 数字三角形)的更多相关文章

  1. POJ3176——Cow Bowling(动态规划)

    Cow Bowling DescriptionThe cows don't use actual bowling balls when they go bowling. They each take ...

  2. [动态规划]数字三角形(版本I-III)

    level 1 1.1题目 1.1.1题目描述 考虑在下面被显示的数字金字塔. 写一个程序来计算从最高点开始在底部任意处结束的路径经过数字的和的最大.每一步可以走到左下方的点也可以到达右下方的点. 在 ...

  3. POJ3176 Cow Bowling 2017-06-29 14:33 23人阅读 评论(0) 收藏

    Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19173   Accepted: 12734 Des ...

  4. POJ3176:Cow Bowling(数字三角形问题)

    地址:http://poj.org/problem?id=3176 题目解析:没什么好说的,之前上课时老师讲过.从下往上找,每一个三角形的顶点可由两个角加上顶点的值 两种方式得到 ,用dp数组保存下最 ...

  5. poj-3176 Cow Bowling &&poj-1163 The Triangle && hihocoder #1037 : 数字三角形 (基础dp)

    经典的数塔模型. 动态转移方程:  dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+p[i][j]; #include <iostream> #include ...

  6. POJ - 3176 Cow Bowling 动态规划

    动态规划:多阶段决策问题,每步求解的问题是后面阶段问题求解的子问题,每步决策将依赖于以前步骤的决策结果.(可以用于组合优化问题) 优化原则:一个最优决策序列的任何子序列本身一定是相当于子序列初始和结束 ...

  7. 动态规划——数字三角形(递归or递推or记忆化搜索)

    动态规划的核心就是状态和状态转移方程. 对于该题,需要用抽象的方法思考,把当前的位置(i,j)看成一个状态,然后定义状态的指标函数d(i,j)为从格子出发时能得到的最大和(包括格子本身的值). 在这个 ...

  8. [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索

    1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...

  9. [ACM_动态规划] 数字三角形(数塔)

    递归方法解决数塔问题 状态转移方程:d[i][j]=a[i][j]+max{d[i+1][j],d[i+1][j+1]} 注意:1\d[i][j]表示从i,j出发的最大总和;2\变界值设为0;3\递归 ...

随机推荐

  1. [sqoop] sqoop 小试牛刀

    sqoop 1.4.6  小试牛刀 sqoop import 参数 1. mysql导入 到hdfs中 ./sqoop import --connect jdbc:mysql://mysql:3306 ...

  2. k8s(6)-滚动更新

    用户希望应用程序始终可用,开发人员应该每天多次部署新版本的应用程序.在Kubernetes中,这是通过滚动更新完成的.滚动更新允许通过使用新的实例逐步更新Pods实例来实现部署的更新,从而实现零停机. ...

  3. 八、Sql Server 基础培训《进度8-查询多种写法》(实际操作)

    知识点: 假设学生表.班级表.年级表 学生表(student) 内码 学生姓名 班级内码 001 张三 1002 002 李四 1002 003 王五 1003 004 钱六 1001 班级表(cla ...

  4. poj1426_kuagnbin带你飞专题一

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30659   Accepted: 127 ...

  5. js判断PC端还是移动端

    function goPAGE() { if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobi ...

  6. git pull总是要输入账号和密码

    如果你用git从远程pull拉取代码,每次都要输入密码,那么执行下面命令即可 git config --global credential.helper store 这个命令则是在你的本地生成一个账号 ...

  7. 原生JS表格行拖动排序,添加了回调功能

    function tableDnD(el, callback) { if (typeof (el) == "string") { el = document.getElementB ...

  8. mysql8.0发布新特性

    2018年4月21日 14:36:42 https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-11.html#mysqld-8-0-11-b ...

  9. day5_不能循环删除list-深拷贝、浅拷贝(import copy)

    一.循环删list里面的元素,会导致下标错位,结果是不对的举例:想删除奇数 l = [1,1,1,2,3,4,5] for i in l: if i%2 !=0: l.remove(i) #删除后,导 ...

  10. 关于STM32时钟系统

    初学STM32,感觉最蛋疼的是它的时钟系统,每次看到它的那个时钟树就有点晕,虽然看了很多这方面的资料,甚至也已经写过很多STM32的模块代码,做过一些小项目,但一直还是对这一块模模糊糊,似懂非懂,所以 ...