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: 

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 ( <= N <= ) rows, determine the highest possible sum achievable.
Input Line : A single integer, N Lines ..N+: Line i+ contains i space-separated integers that represent row i of the triangle.
Output Line : The largest sum achievable using the traversal rules
Sample Input
Sample Output

刚差不多刚看完动态规划,把小白书的例题大半都看懂了,也查了不少博客,然而做起题目来,连状态都不太会定义。

于是乎又查了起来,嗯看到了别人对状态的定义: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[][];
int main()
{
int n;
cin>>n;
for (int i = ; i <=n; i++)
for (int j = ; j <=i; j++)
cin>>num[i][j];
for (int i = n; i >= ; i--)
for (int j = ; j <= i; j++)
num[i][j] += max(num[i+][j], num[i+][j+]);
cout<<num[][]<<endl; return ;
}

最新文章

  1. 在Centos中部署redis运行状态图形化监控工具 — RedisLive
  2. jquery自定义滚动条 鼠标移入或滚轮时显示 鼠标离开或悬停超时时隐藏
  3. Fragment全解析系列(三):Fragment之我的解决方案:Fragmentation
  4. 非静态的字段、方法或属性“System.Web.UI.Page.ClientScript...”要求对象引用 (封装注册脚本)
  5. &lt;iframe&gt;标签的一些说明
  6. Geometric Shapes - POJ 3449(多边形相交)
  7. 如何用angularjs制作一个完整的表格之五__完整的案例
  8. string can not be resolved
  9. 8. Andr&#233;nalin ★ Serial
  10. Crash工具实战-变量解析【转】
  11. (转)SQLServer查询数据库各种历史记录
  12. BZOJ4170:极光(CDQ分治)
  13. query 中 radio选中小技巧
  14. Mac eclipse 连接安卓手机调试 adb
  15. 读取excel的方法(可用于批量导入)
  16. 如何安装及使用PuTTY
  17. LInux50个基本命令
  18. 爆款PHP面试题
  19. Django开发流程
  20. Python 时间日历类型

热门文章

  1. 创建服务消费者(Ribbon)
  2. 3015C语言_流程设计
  3. python 方法无法在线程中使用(附python获取网络流量)
  4. 快速理解类的访问控制(public,protected,private)
  5. 移动端使用rem.js,解决rem.js 行内元素占位问题
  6. node.js简单数据接口开发
  7. spring 5.x 系列第18篇 —— 整合websocket (代码配置方式)
  8. js 数组去重方法
  9. HDU 1286:找新朋友(欧拉函数)
  10. JSON.stringify() 的深入理解