POJ-3176 Cow Bowling(基础dp)
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
刚差不多刚看完动态规划,把小白书的例题大半都看懂了,也查了不少博客,然而做起题目来,连状态都不太会定义。
于是乎又查了起来,嗯看到了别人对状态的定义:way[i][j]表示以第i行j列的位置作为终点的路线的最大权值。 (注意区分初始化时的意义)
好的开始自己写状态转移方程:dp[i][j] = a[i][j] + max( dp[i+1][j], dp[i+1][j+1])
后来发现别人的更加简单:num[i][j] += max(num[i+1][j], num[i+1][j+1])
由于递推是沿着三角形向上的,就直接在原来的值上更新,只是得注意区分初始化时的意义
附上AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int num[355][355];
int main()
{
int n;
cin>>n;
for (int i = 1; i <=n; i++)
for (int j = 1; j <=i; j++)
cin>>num[i][j];
for (int i = n; i >= 1; i--)
for (int j = 1; j <= i; j++)
num[i][j] += max(num[i+1][j], num[i+1][j+1]);
cout<<num[1][1]<<endl; return 0;
}
POJ-3176 Cow Bowling(基础dp)的更多相关文章
- poj 3176 Cow Bowling(dp基础)
Description The cows don't use actual bowling balls when they go bowling. They each take a number (i ...
- poj 3176 Cow Bowling(区间dp)
题目链接:http://poj.org/problem?id=3176 思路分析:基本的DP题目:将每个节点视为一个状态,记为B[i][j], 状态转移方程为 B[i][j] = A[i][j] + ...
- 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 1163 The Triangle &poj 3176 Cow Bowling (dp)
id=1163">链接:poj 1163 题意:输入一个n层的三角形.第i层有i个数,求从第1层到第n层的全部路线中.权值之和最大的路线. 规定:第i层的某个数仅仅能连线走到第i+1层 ...
- POJ 3176 Cow Bowling
Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13016 Accepted: 8598 Desc ...
- POJ 3176 Cow Bowling (水题DP)
题意:给定一个金字塔,第 i 行有 i 个数,从最上面走下来,只能相邻的层数,问你最大的和. 析:真是水题,学过DP的都会,就不说了. 代码如下: #include <cstdio> #i ...
- POJ - 3176 Cow Bowling 动态规划
动态规划:多阶段决策问题,每步求解的问题是后面阶段问题求解的子问题,每步决策将依赖于以前步骤的决策结果.(可以用于组合优化问题) 优化原则:一个最优决策序列的任何子序列本身一定是相当于子序列初始和结束 ...
- 【POJ 3176】Cow Bowling(DP)
题 Description The cows don't use actual bowling balls when they go bowling. They each take a number ...
- poj 2184 Cow Exhibition(dp之01背包变形)
Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - ...
- poj 3616 Milking Time (基础dp)
题目链接 http://poj.org/problem?id=3616 题意:在一个农场里,在长度为N个时间可以挤奶,但只能挤M次,且每挤一次就要休息t分钟: 接下来给m组数据表示挤奶的时间与奶量求最 ...
随机推荐
- oeasy教您玩转 linux 010213 中文 fcitx
我们来回顾一下 上一部分我们都讲了什么? 管道 ls | cowsay 管道的符号是| 管道的作用是连接 原来应该输出到屏幕的内容 通过管道流到了另一个命令做为参数 这次是否可以让cow说出一些中文 ...
- 题解:AT_abc360_c [ABC360C] Move It
背景 机房大佬掉大分了,乐悲. 题意 给你几个箱子和每个箱子里装有的东西 \(a\) 和对应的重量 \(w\),现在要让每个箱子里都装有一个东西,每次可以移动任意一个箱子中的任意一个东西,代价为它的重 ...
- 写写stream流的终结操作
终结操作和中间操作的区别:中间操作返回的一直都是stream,所以可以一直使用,但是终结操作返回的不是stream,后面不能继续操作 foreach:对流中的所有元素进行遍历操作 count:获取当前 ...
- h5py文件写入之——flush和update
技术背景 在前面的一篇博客中,我们介绍过使用VMD可视化H5MD标准化格式的轨迹文件的方法.H5MD本质上就是一个有规范格式的hdf5二进制文件,本文主要介绍两个关于hdf5的内容更新操作. 写入和更 ...
- Fidder响应数据SyntaxView乱码的处理方法
当Fidder查看响应数据"SyntaxView"出现乱码时,可以点击上方菜单栏的"Decode"按钮,等"Decode"出现蓝色边框后再重 ...
- Jmeter函数助手38-isVarDefined
isVarDefined函数用于判断变量是否存在. 变量的名称:填入变量名称.如果变量存在返回true,如果不存在返回false 1.先一些定义变量 ${__isVarDefined(now)},no ...
- 【Uni-App】API笔记 P1
1.调试打印: console.log() 向控制台打印 log 日志 console.debug() 向控制台打印 debug 日志 注:App 端自定义组件模式下,debug 方法等同于 log ...
- 【Spring-Security】Re09 CSFR处理
一.CSRF: CSRF 全称 Cross Site Request Forgery 跨站请求伪造 又称为OneClick Attack & SessionRiding 是非法请求访问,通过伪 ...
- gym库中from gym.wrappers import FlattenObservation的理解
看代码的过程中看到有这样的调用: from gym.wrappers import FlattenObservation if sinstance(env.observation_space, gym ...
- Linux磁盘/硬盘测速,dd命令
参考: https://blog.csdn.net/Franciz777/article/details/126779259 ===================================== ...