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组数据表示挤奶的时间与奶量求最 ...
随机推荐
- 手动设置提示在此环境中不可导入Django
手动设置提示在此环境中不可导入Django 环境参数添加manage.py中的代码'DJANGO_SETTINGS_MODULE', 'codeProject.settings'
- vue3+elementplus 去除小数点后多余的0公用函数
vue3+elementplus 去除小数点后多余的0公用函数 export function removeTrailingZeros(value) { // 尝试将值转换为数字 const nume ...
- oeasy教您玩转vim - 005 - # 程序本质
程序本质 回忆上次内容 py 的程序是按照顺序 一行行挨排解释执行的 我们可以 python3 -m pdb hello.py 来对程序调试 调试的目的是去除 bug 别害怕 bug bug 会有 ...
- git常用代码
//当前文件夹删除的文件恢复git reset Head .// 查看所有分支git branch// 查看本地分支 对应的远程分支git branch -vv//git branch -vv//创建 ...
- 简单写写IO流里的一些高级流
缓冲流Buffered:缓冲流是一种高级的流,他可以对file类的流进行包装,内部含有一种缓冲池,可以在一定程度上提高IO的读写效率 不过,按实验来看,只要给低级流和缓冲流相似的byte读写,其实两者 ...
- nats 简介和使用
nats 简介和使用 nats 有 3 个产品 core-nats: 不做持久化的及时信息传输系统 nats-streaming: 基于 nats 的持久化消息队列(已弃用) nats-jetstre ...
- Mac端MySQL安装教程
Mac端: 1.安装 ** 首先进入官网:**https://dev.mysql.com/downloads/mysql/ 根据自身情况是M系列芯片(ARM版)还是Intel系列芯片(x ...
- RedisTemplate使用rightPushAll时的注意事项
问题:第一次使用时rightPushAll,我以为这个方法就是直接把我们集合中的数据全部添加到redis的list里面,但是如果直接使用ArrayList类型添加,发现事情并不是我们想的这样,他并没有 ...
- 对比python学julia(第一章)--(第四节)冰雹猜想
4.1 依葫芦画瓢 冰雹猜想是一种非常有趣的数字黑洞,曾让无数的数学爱好者为之痴迷.它有一个非常简单的变换规则,具体来说就是:任意取一个正整数n,如果n是偶数,就把n变成n/2;如果n是奇数,就把n变 ...
- 【DataBase】MySQL 03 基本命令 & 语法规范
参考至视频:P12 - P15 https://www.bilibili.com/video/BV1xW411u7ax?p=82 SHOW 和相关的基本命令 查看所有的数据库 SHOW DATABAS ...