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.

 
渐渐对动态规划有所感悟 加油!
 #include <iostream>
#include <algorithm>
using namespace std;
int a[][];
int n;
int dp[][];
int main()
{
while(cin>>n){
for(int i=;i<n;i++){
for(int j=;j<=i;j++){
cin>>a[i][j];
}
}
int res=;
dp[][]=a[][];
for(int i=;i<n;i++){
for(int j=;j<=i;j++){
if(j==) dp[i][j]=dp[i-][j]+a[i][j];//第一列只有一个方向累加
else dp[i][j]=max(dp[i-][j],dp[i-][j-])+a[i][j];
res=max(res,dp[i][j]);
}
}
cout<<res<<endl;
}
return ;
}

另一种写法:倒推,感觉更清晰一点

 #include <iostream>
#include <algorithm>
using namespace std;
int n;
int a[][];
int dp[][];
int main()
{
while(cin>>n){
for(int i=;i<n;i++){
for(int j=;j<=i;j++){
cin>>a[i][j];
dp[i][j]=a[i][j];
}
}
for(int i=n-;i>=;i--){
for(int j=;j<=i;j++){
dp[i][j]+=max(dp[i+][j+],dp[i+][j]);
}
}
cout<<dp[][]<<endl;
}
return ;
}

最新文章

  1. Android 自定义View实现多行RadioGroup (MultiLineRadioGroup)
  2. Android中使用logwrapper来重定向应用程序的标准输出
  3. 【数论,找规律】Uva 11526 - H(n)
  4. 论Oracle字符集“转码”过程
  5. WPF Paragraph获取或修改文本内容
  6. 转载——SQL Server数据库性能优化之SQL语句篇
  7. 【Java每日一题】20170111
  8. Java编程风格学习(三)
  9. s​q​l​ ​s​e​r​v​e​r​ ​2​0​0​0​登​录​名​与​数​据​库​用​户​名​的​关​联​问​题
  10. MySQL主从同步和读写分离的配置
  11. requests_模拟登录知乎
  12. Idea工具开发 SpringBoot整合JSP(毕设亲测可用)
  13. Java IO--字符流--BufferedReader和BufferedWriter
  14. shell的进度条【转】
  15. Mapbox浅析(快速入门Mapbox)
  16. log4j控制指定包下的日志
  17. [Unity][安卓]Unity和Android Studio 3.0 交互通讯(1)Android Studio 3.0 设置
  18. bootstrap table表格前台分页,点击tab选项,重新刷新表格
  19. C++重写(覆盖)、重载、重定义、
  20. Tencent QQ现在就是一个十八层地狱下面的大恶魔-删除右键里的&quot;通过QQ发送到&quot;

热门文章

  1. nginx 内置变量
  2. 编译器处理警告、错误 #pragma GCC diagnostic ignored &quot;-Wunused&quot;
  3. 数据库更新锁WITH UPDLOCK
  4. js如何获取字符串第几次出现的位置
  5. java可供判断某字符串是什么编码的一行代码
  6. macOS Sierra上面的php开发环境安装
  7. python全栈开发 * 33 知识点汇总 * 180718
  8. python全栈开发 * 15知识点汇总 * 180621
  9. F#周报2019年第7期
  10. .NET Core 的 Span&lt;T&gt; 学习与使用笔记