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. An exception occurred during a WebClient request

    System.Net.WebException was caught HResult=-2146233079 Message=An exception occurred during a WebCli ...

  2. STL---deque(双端队列)

    Deque是一种优化了的.对序列两端元素进行添加和删除操作的基本序列容器.它允许较为快速地随机访问,但它不像vector 把所有的对象保存在一块连续的内存块,而是采用多个连续的存储块,并且在一个映射结 ...

  3. 时间和日期控件(Calendar1)

    取得选择的: taskItem["data"] = Calendar1.SelectedDate.ToShortDateString();

  4. wxpython 基本的控件 (文本)

    wxPython 工具包提供了多种不同的窗口部件,包括了本章所提到的基本控件.我们涉及静态文本.可编辑的文本.按钮.微调.滑块.复选框.单选按钮.选择器.列表框.组合框和标尺.对于每种窗口部件,我们将 ...

  5. ios swift 2 新的OptionSetType使用方法

    http://www.rockhoppertech.com/blog/swift-2-optionsettype/?utm_source=tuicool 主要使用方法如下 components([NS ...

  6. properties配置文件的读取和写入

    /** * 类名:PropertiesUtil * 功能:提供对properties配置文件的读取和写入 * @author ChengTao */package com.xy.xyd.rest.bi ...

  7. 【leetcode】 Unique Binary Search Trees (middle)☆

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  8. WinForm轻松实现自定义分页 (转载)

    转载至http://xuzhihong1987.blog.163.com/blog/static/267315872011315114240140/ 以前都是做web开发,最近接触了下WinForm, ...

  9. HDU 4387 Stone Game (博弈)

    题目:传送门. 题意:长度为N的格子,Alice和Bob各占了最左边以及最右边K个格子,每回合每人可以选择一个棋子往对面最近的一个空格移动.最先不能移动的人获得胜利. 题解: k=1时 很容易看出,n ...

  10. Java发送邮件初窥

    一.背景 最近朋友的公司有用到这个功能,之前对这一块也不是很熟悉,就和他一起解决出现的异常的同时,也初窥一下使用Apache Common Email组件进行邮件发送. 二.Java发送邮件的注意事项 ...