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)的更多相关文章

  1. poj 3176 Cow Bowling(dp基础)

    Description The cows don't use actual bowling balls when they go bowling. They each take a number (i ...

  2. poj 3176 Cow Bowling(区间dp)

    题目链接:http://poj.org/problem?id=3176 思路分析:基本的DP题目:将每个节点视为一个状态,记为B[i][j], 状态转移方程为 B[i][j] = A[i][j] + ...

  3. POJ 3176 Cow Bowling(dp)

    POJ 3176 Cow Bowling 题目简化即为从一个三角形数列的顶端沿对角线走到底端,所取得的和最大值 7 * 3 8 * 8 1 0 * 2 7 4 4 * 4 5 2 6 5 该走法即为最 ...

  4. poj 1163 The Triangle &amp;poj 3176 Cow Bowling (dp)

    id=1163">链接:poj 1163 题意:输入一个n层的三角形.第i层有i个数,求从第1层到第n层的全部路线中.权值之和最大的路线. 规定:第i层的某个数仅仅能连线走到第i+1层 ...

  5. POJ 3176 Cow Bowling

    Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13016   Accepted: 8598 Desc ...

  6. POJ 3176 Cow Bowling (水题DP)

    题意:给定一个金字塔,第 i 行有 i 个数,从最上面走下来,只能相邻的层数,问你最大的和. 析:真是水题,学过DP的都会,就不说了. 代码如下: #include <cstdio> #i ...

  7. POJ - 3176 Cow Bowling 动态规划

    动态规划:多阶段决策问题,每步求解的问题是后面阶段问题求解的子问题,每步决策将依赖于以前步骤的决策结果.(可以用于组合优化问题) 优化原则:一个最优决策序列的任何子序列本身一定是相当于子序列初始和结束 ...

  8. 【POJ 3176】Cow Bowling(DP)

    题 Description The cows don't use actual bowling balls when they go bowling. They each take a number ...

  9. poj 2184 Cow Exhibition(dp之01背包变形)

    Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - ...

  10. poj 3616 Milking Time (基础dp)

    题目链接 http://poj.org/problem?id=3616 题意:在一个农场里,在长度为N个时间可以挤奶,但只能挤M次,且每挤一次就要休息t分钟: 接下来给m组数据表示挤奶的时间与奶量求最 ...

随机推荐

  1. 10、SpringMVC之处理Ajax请求

    创建名为spring_mvc_ajax的新module,过程参考9.1节和9.5节 10.1.SpringMVC处理Ajax请求 10.1.1.页面请求示例 <input type=" ...

  2. 【C3】06 选择器概述

    CSS中,选择器用来指定网页上我们想要样式化的HTML元素. CSS选择器有很多种可供使用,所以在选择要样式化的元素时,我们可以做到很精细的地步. 本文和本文的子篇中,我们将会很详细地讲授选择器不同的 ...

  3. JavaWeb入门到实战学习笔记

    了解,讲得并不是很好,很展开. 概念 动态web Web服务器 web服务器这节也是蜻蜓点水,引出tomcat而已 ASP(C#语言,微软) JSP PHP Java bootstrapclasslo ...

  4. 工业机器人的力控(Force Control)

    相关: https://baijiahao.baidu.com/s?id=1785676027803650068 机器人编程人员需要提前知道机器人的摩擦力.阻力.质量.重力,等数值,然后建立基于物理模 ...

  5. 【转载】 深入理解TensorFlow中的tf.metrics算子

    原文地址: https://mp.weixin.qq.com/s/8I5Nvw4t2jT1NR9vIYT5XA ============================================ ...

  6. 【转载】 Python格式化字符串f-string概览

    版权声明:本文为CSDN博主「sunxb10」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/sunxb10/a ...

  7. vscode配置docker插件

    参考: https://zhuanlan.zhihu.com/p/530413695 ================================================ 本文不对vsco ...

  8. Numb 题解

    前言 五一网课的例题,但是网上没有题解,所以来写一篇,就当攒 RP 了.题目可以在这里提交.原题是 Baekjoon - 19083,但是交不了? 题目简述 给你一个偶数 \(n\),求一个二进制数 ...

  9. LVS-TUN隧道模式

    当然可以.以下是按照您的要求整理的表格形式的实验手册: 主机名称 网卡信息 安装应用 系统 Client客户端 192.168.2.101 无 RHEL8/CentOS8 Lvs服务器(DR) DIP ...

  10. MySQL 优化慢查询

    查询以SELECT 语句的形式执行数据库中的所有查找操作.调整这些语句是重中之重,无论是实现动态网页的亚秒响应时间,还是缩短数小时生成大量夜间报告的时间. 此外SELECT语句,进行查询调谐技术也适用 ...