Poj3176 Cow Bowling (动态规划 数字三角形)
Description
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
Lines 2..N+1: Line i+1 contains i space-separated integers that represent row i of the triangle.
Output
Sample Input
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Sample Output
30
Hint
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 (动态规划 数字三角形)的更多相关文章
- POJ3176——Cow Bowling(动态规划)
Cow Bowling DescriptionThe cows don't use actual bowling balls when they go bowling. They each take ...
- [动态规划]数字三角形(版本I-III)
level 1 1.1题目 1.1.1题目描述 考虑在下面被显示的数字金字塔. 写一个程序来计算从最高点开始在底部任意处结束的路径经过数字的和的最大.每一步可以走到左下方的点也可以到达右下方的点. 在 ...
- POJ3176 Cow Bowling 2017-06-29 14:33 23人阅读 评论(0) 收藏
Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19173 Accepted: 12734 Des ...
- POJ3176:Cow Bowling(数字三角形问题)
地址:http://poj.org/problem?id=3176 题目解析:没什么好说的,之前上课时老师讲过.从下往上找,每一个三角形的顶点可由两个角加上顶点的值 两种方式得到 ,用dp数组保存下最 ...
- 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 ...
- POJ - 3176 Cow Bowling 动态规划
动态规划:多阶段决策问题,每步求解的问题是后面阶段问题求解的子问题,每步决策将依赖于以前步骤的决策结果.(可以用于组合优化问题) 优化原则:一个最优决策序列的任何子序列本身一定是相当于子序列初始和结束 ...
- 动态规划——数字三角形(递归or递推or记忆化搜索)
动态规划的核心就是状态和状态转移方程. 对于该题,需要用抽象的方法思考,把当前的位置(i,j)看成一个状态,然后定义状态的指标函数d(i,j)为从格子出发时能得到的最大和(包括格子本身的值). 在这个 ...
- [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索
1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...
- [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\递归 ...
随机推荐
- Thread线程中断相关方法
public class Demo { /* * 线程中断相关方法 * ---------------------------------------------------------------- ...
- Rafy环境配置
如果现在项目已经创建好啦,要使用Rafy框架进行存储,这里我简单的总结下配置的环境步骤: 一.添加引用Rafy的SDK,如下几个; 二.新建文件夹Entities 在此文件夹下创建rafy实体以及仓库 ...
- 远程连接服务器影像文件进行服务发布以及问题解决【the data item is inaccessible】
场景模拟: 本机安装有arcgis desktop以及arcgis server10.1,server的站点账号为arcgis. 需要发布影像服务并进行切片,使用的影像数据存放在远程服务器上,影像较大 ...
- 【Zookeeper系列】ZooKeeper安装配置(转)
原文链接:https://www.cnblogs.com/sunddenly/p/4018459.html 一.Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪 ...
- 将VSCode添加到右键
https://www.cnblogs.com/Rexcnblog/p/8046371.html https://www.jianshu.com/p/b49002fa10a7 @echo Off :S ...
- springboot源码解读01
package org.springframework.web; @javax.servlet.annotation.HandlesTypes({org.springframework.web.Web ...
- python数据库操作
python操作数据库应该比java更简单些,连接数据库需要有驱动,pcat就推荐自己遇到的3个模块:mysql.connector.sqlalchemy.MySQLdb
- 24.redux
Flux:Flux 是一种架构思想 https://facebook.github.io/flux/ 官网 资料: http://www.ruanyifeng.com/blog/2016/01/flu ...
- 【转载】解决gridview空行时不显示的问题
问题: GridView控件应用很是广泛,通常将它与DataSourceControl搭配使用,当然也可以手工指定DataSource属性来完成数据绑定.如果数据源返回一个空行的数据集(例如查询不到指 ...
- java中的静态代理和动态代理
1.动态代理的定义:为其他对象提供一个代理以控制对这个对象的访问 代理类主要负责委托类的预处理消息,过滤消息,把消息传给委托类以及消息事后处理 按照代理类的创建时期,代理类可以分为2种:静态代理类(在 ...