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\递归 ...
随机推荐
- 多级分类标签{dede:channelartlist}实现当前栏目颜色高亮显示
我们知道,如果要在网站上多级分类显示的话,可以用下面的标签实现: {dede:channelartlist typeid='4' cacheid='channelsonlist'} < ...
- Oracle 10G 安装文档
Oracle 10G安装指导 1. 解压文件10201_database_win32.zip,并双击解压目录下的setup.exe,出现安装界面,如下: 2. 输入口令和确认口令,如:password ...
- [EMSE'17] A Correlation Study between Automated Program Repair and Test-Suite Metrics
Basic Information Authors: Jooyong Yi, Shin Hwei Tan, Sergey Mechtaev, Marcel Böhme, Abhik Roychoudh ...
- [原]在使用ubuntu14.04,安装devstack的时候报错./stack.sh: line 463: generate-subunit: command not found
=======在使用ubuntu14.04,安装devstack的时候报错./stack.sh: line 463: generate-subunit: command not found 2016- ...
- 【CF446D】DZY Loves Games 高斯消元+矩阵乘法
[CF446D]DZY Loves Games 题意:一张n个点m条边的无向图,其中某些点是黑点,1号点一定不是黑点,n号点一定是黑点.问从1开始走,每次随机选择一个相邻的点走过去,经过恰好k个黑点到 ...
- VC++ 字符串Dword、LPSTR、LPWSTR、LPCSTR、LPCWSTR、LPTSTR、LPCTSTR
类 型 MBCS UNICODE TCHAR char char WCHAR wchar_t wchar_t LPSTR char* char* LPCSTR const ...
- E - Closest Common Ancestors
Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u, ...
- linux基础操作1
date #查看日期cal #查看当月日历cal 2017 #查看某年全年的日历whoami #查看当前登录的用户 #修改网卡,可以上网的步骤vi /etc/sysconfig/network-scr ...
- xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun
原文地址 xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcru ...
- Ubuntu12.04下解决sudo apt-get update警告Duplicate sources.list entry
sudo apt-get update,会提示如下警告: W: Duplicate sources.list entry http://archive.canonical.com/ubuntu/ pr ...