POJ 3176 简单DP
Cow Bowling
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 16448 Accepted: 10957
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.
题意:一个n层的三角形,从第一行第一列开始向下走,每次只能向下一行且只能走到与之到相邻的两块,求到最后一行路程的最大值。
解题思路:动规
f[i][j]表示到第i行第j列路程的最大值
走到第i行第j列的路程只能与走到第i-1行第j列和第i-1行第i-1列有关
不难写出状态转移方程为:f[i][j]=max(f[i][j]+f[i-1][j-1],f[i][j]+f[i-1][j]);
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,f[666][666];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
scanf("%d",&f[i][j]);
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
f[i][j]=max(f[i][j]+f[i-1][j-1],f[i][j]+f[i-1][j]);
}
}
for(int i=1;i<=n;i++)
{
if(f[n][i]>f[0][0]) f[0][0]=f[n][i];
}
printf("%d",f[0][0]);
}
POJ 3176 简单DP的更多相关文章
- poj 1579 简单dp由下往上
#include<stdio.h> #include<string.h> #define N 22 int dp[N][N][N]; int main() { int n,m, ...
- POJ 3176 Cow Bowling(dp)
POJ 3176 Cow Bowling 题目简化即为从一个三角形数列的顶端沿对角线走到底端,所取得的和最大值 7 * 3 8 * 8 1 0 * 2 7 4 4 * 4 5 2 6 5 该走法即为最 ...
- poj 1157 LITTLE SHOP_简单dp
题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...
- poj 1163 The Triangle &poj 3176 Cow Bowling (dp)
id=1163">链接:poj 1163 题意:输入一个n层的三角形.第i层有i个数,求从第1层到第n层的全部路线中.权值之和最大的路线. 规定:第i层的某个数仅仅能连线走到第i+1层 ...
- 【POJ - 2533】Longest Ordered Subsequence (最长上升子序列 简单dp)
Longest Ordered Subsequence 搬中文 Descriptions: 给出一个序列,求出这个序列的最长上升子序列. 序列A的上升子序列B定义如下: B为A的子序列 B为严格递增序 ...
- poj1189 简单dp
http://poj.org/problem?id=1189 Description 有一个三角形木板,竖直立放.上面钉着n(n+1)/2颗钉子,还有(n+1)个格子(当n=5时如图1).每颗钉子和周 ...
- poj1163The Triangle(简单DP)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj ...
- POJ1088:滑雪(简单dp)
题目链接: http://poj.org/problem?id=1088 题目要求: 一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小.求可以滑落的最长长度. 题目解析: 首先要先排一 ...
- HDU 1087 简单dp,求递增子序列使和最大
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
随机推荐
- ConsensusClusterPlus根据基因表达量对样品进行分类
#http://www.ncbi.nlm.nih.gov/pmc/articles/PMC2881355/ 一致聚类方法,采用重抽样方法来验证聚类合理性. library(ALL)data(ALL)d ...
- select框内容的编辑、修改、添加、删除操作
// 添加 function col_add() { var selObj = $("#mySelect"); var value="value"; var t ...
- 《BI那点儿事》Cube的存储
关系 OLAP (ROLAP)ROLAP的基本数据和聚合数据均存放在关系数据库中:ROLAP 存储模式使得分区的聚合存储在关系数据库的表(在分区数据源中指定)中.但是,可为分区数据使用 ROLAP 存 ...
- jQuery核心之 $
参考jQuery官网API文档 $ 和 $() 的区别很重要: 1.$(document).ready() 和 $(document).load() 的 区别: 前者等到DOM准备好了之后就会 ...
- 用Python编写的第一个回测程序
用Python编写的第一个回测程序 2016-08-06 def savfig(figureObj, fn_prefix1='backtest8', fn_prefix2='_1_'): import ...
- iOS9 collectionView新特性
近日因为系统升级导致xcode6.系列版本出现bug,于是开始使用xcode7.在使用之余突然想到collectionView在iOS9中发布了一个可以移动cell的新特性,就尝试着将其实现,无奈ap ...
- hdu4352 XHXJ's LIS
链接 这个题最不好想到的是状态的保存,也没有几亿的数组让你开,怎么保存前面出现了哪些数字. 题意让你求最长上升子序列的长度为k的数字的数目,可以是不连续的,可以保留一个状态栈,栈顶部依次更新,再保证长 ...
- window.location.href 失效的解决办法
第一种:在window.location.href 后面加上 window.event.returnValue = false; 如: <a href="#" onclick ...
- Uncaught SecurityError: Failed to execute 'replaceState' on 'History': A history state object with
stackoverflow上的的解决方法:Install a web server and load your HTML document from there.
- Construct Binary Tree from Preorder and Inorder Traversal [LeetCode]
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...