Description

7
3 8
8 1 0
2 7 4 4
4 5 2 6 5 (Figure 1)

Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.

Input

Your program is to read from standard input. The first line contains one integer N: the number of rows in the triangle. The following N lines describe the data of the triangle. The number of rows in the triangle is > 1 but <= 100. The numbers in the triangle, all integers, are between 0 and 99.

Output

Your program is to write to standard output. The highest sum is written as an integer.

Sample Input

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

Sample Output

30

题解:最基础的dp,直接记录状态

dp[i][j]表示以第i行第j列开头的三角形的最大值

dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+a[i][j];

(1) 从后向前递推

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

(2) 从前向后递推

 #include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = ;
int dp[MAXN][MAXN];
int main()
{
int n;
cin >> n;
for (int i = ; i < n;i++)
for (int j = ; j <= i; j++)
cin >> dp[i][j];
for (int i = ; i < n;i++)
for (int j = ; j <= i; j++)
{
if (i==)continue;
else if (j == )dp[i][j] = dp[i-][j] + dp[i][j];
else if (j == i)dp[i][j] = dp[i-][j-] + dp[i][j];
else dp[i][j] = max(dp[i-][j],dp[i-][j-]) + dp[i][j];
}
int ans = ;
for (int j = ; j < n; j++)
ans = max(ans,dp[n-][j]);
cout << ans;
return ;
}

POJ 1163:The Triangle的更多相关文章

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

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

  2. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  3. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  4. 【九度OJ】题目1163:素数 解题报告

    [九度OJ]题目1163:素数 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1163 题目描述: 输入一个整数n(2< ...

  5. OpenJudge/Poj 1163 The Triangle

    1.链接地址: http://bailian.openjudge.cn/practice/1163 http://poj.org/problem?id=1163 2.题目: 总时间限制: 1000ms ...

  6. POJ 1163 The Triangle【dp+杨辉三角加强版(递归)】

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 49955   Accepted: 30177 De ...

  7. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  8. POJ 1163 The Triangle 简单DP

    看题传送门门:http://poj.org/problem?id=1163 困死了....QAQ 普通做法,从下往上,可得状态转移方程为: dp[i][j]= a[i][j] + max (dp[i+ ...

  9. Poj 1163 The Triangle 之解题报告

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42232   Accepted: 25527 Description 7 3 ...

随机推荐

  1. [20160704]Addition program that use JOptionPane for input and output

    //Addition program that use JOptionPane for input and output. import javax.swing.JOptionPane; public ...

  2. 一个很详细的web.xml讲解

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "- ...

  3. centos7精简版(minimal)killall: command not found

    centos7精简版(minimal)运行killall命令提示 command not found 是由于没有安装psmisc所致 Psmisc软件包包含三个帮助管理/proc目录的程序. 安装下列 ...

  4. ACM/ICPC 之 Bellman Ford练习题(ZOJ1791(POJ1613))

    这道题稍复杂一些,需要掌握字符串输入的处理+限制了可以行走的时间. ZOJ1791(POJ1613)-Cave Raider //限制行走时间的最短路 //POJ1613-ZOJ1791 //Time ...

  5. 7.js模式-装饰者模式

    1. 装饰者模式 给对象动态增加职责的方式称为装饰者模式. Function.prototype.before = function(beforefn){ var _self = this; retu ...

  6. 【linux】学习2

    鸟哥那本书的第6章 文件权限: ^                ^     ^      ^        ^              ^                 ^ 1         ...

  7. GoF23种设计模式

    创建型模式 1.ABSTRACT FACTORY-追MM少不了请吃饭了,麦当劳的套餐和肯德基的套餐都是MM爱吃的东西,虽然口味有所不同,但不管你带MM去麦当劳或肯德基,只管向服务员说"两个B ...

  8. Servlet题库

    一.    填空题 Servlet中使用Session对象的步骤为:调用  HttpServletRequest.getSession()  得到Session对象,查看Session对象,在会话中保 ...

  9. python基础——使用@property

    python基础——使用@property 在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把成绩随便改: s = Student() s.score = 9 ...

  10. StoryBoard和代码结合 按比例快速兼容iPhone6/6 Plus教程

     转:http://www.cocoachina.com/ios/20141230/10800.html 编者注:根据网友们的评论,文章中的方法有很大的局限性,请谨慎使用! 现在由于苹果公司出了6和6 ...