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. 一个Try多个Catch需要注意的事项

    一个程序包含一个try块和两个catch块,两个catch子句都有能力捕捉一个try块发出的异常,若两个catch子句次序不同时程序结果会发生变化吗? 一个try块后有两个catch块,这很正常,因为 ...

  2. 使用SharePoint 2010 母版页

    SharePoint 2010母版页所用的还是ASP.NET 2.0中的技术.通过该功能,实现了页面框架布局与实际内容的分离.虽然在本质上自定义母版页的过程和以前版本的SharePoint大致相同,但 ...

  3. js 为label标签和div标签赋值

    <label id="ttile"></label> document.getElementById('ttile').innerText="&q ...

  4. 【leetcode】Word Ladder

    Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...

  5. POJ 1503

    http://poj.org/problem?id=1503 对于这个题我也是醉了,因为最开始是有学长和我们说过这个题目的,我以为我记得题目是什么意思,也就没看题目,结果按案例去理解题意,结果WA了一 ...

  6. windows下打开VMware虚拟机时提示内存不足的处理方法

    参考:http://thinkpig007.blog.51cto.com/971471/1589831 以管理员身份运行vmware.exe即可 错误的错误提示: Not enough physica ...

  7. iOS gcd dispatch使用注意,dispatch_syn可能产生的死锁

      我们在使用dispatch_sync 时可能会出现死锁,看下面的例子: import UIKit class ViewController: UIViewController { var seri ...

  8. zpf 路由功能

    2015-4-11 20:51:06 又搞了一天, 解决了一堆bug, 重新规划了类文件夹, 改善自动加载功能 最新的特性就是支持子域名路由了 因为整个框架还在完善当中, 而且里边有好多接口有我自己申 ...

  9. XLL 框架库中的函数

    这个框架库,可以让编写 XLL 更加容易.包含了管理 XLOPER/XLOPER12 内存的简单函数,创建临时 XLOPER/XLOEPR12 ,强制调用回调函数 (Excel4,Excel4v,Ex ...

  10. TFS强制解除签入签出锁定

    首先你要知道你服务器的用户名和登录密码 看被别人签出了的文件