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. C# 数组、多维数组(矩形数组)、锯齿数组(交叉数组)

    数组是变量的索引列表,可以在方括号中指定索引来访问数组中的各个成员,其中索引是一个整数,从0开始. 一维数组 多维数组(矩形数组) 数组的数组(锯齿数组) 数组必须在访问之前初始化,数组的初始化有两种 ...

  2. MySQL ODBC for Linux

    参考自http://blog.csdn.net/allens_zhou/article/details/8575400 centos7 64bit [IP:192.168.0.100] yum ins ...

  3. cocos2dx旧版本支持arm64修改

    修改的版本是cocos2dx.2.2 1.在neon_matrix_impl.c中修改 #if defined(__ARM_NEON__)为 #if defined(_ARM_ARCH_7) 2.在m ...

  4. Divide and conquer:4 Values whose Sum is 0(POJ 2785)

    找四个数的和为0 题目大意:给定四个集合,要你每个集合选4个数字,组成和为0 这题是3977的简单版,只要和是0就可以了 #include <iostream> #include < ...

  5. C++库(Thrift)

    Thrift通信框架 0 简介 Thrift是一个软件通讯框架,用来进行可扩展且跨语言的服务的开发,最初由Facebook于2007年开发,2008年进入Apache开源项目.它结合了功能强大的软件堆 ...

  6. code vs1517 求一次函数解析式(数论 纯数学知识)

    1517 求一次函数解析式  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver 题解  查看运行结果     题目描述 Description 相信大家都做过练 ...

  7. 细谈WEB标准

    最近有些朋友很是疑惑web标准是什么,我在这里先做一个总结,有更好的见解的可以私信给我! 首先切入正题之前先谈一下什么叫DOCTYPE,DOCTYPE是document type(文档类型)的简写,主 ...

  8. Java version 32转64位

    本来在cmd中 输入 java -version后显示 为 32位,现在需要转为64 位因为eclipse 不兼容,方法: ()下载 java 1.8 64位,安装,然后 ()进入环境变量 发现有两个 ...

  9. 根据复选框checkbox的选中状态来打开或关闭隐藏层

    HTML:  <input type="checkbox" id="check-expert"> <div id="expert&q ...

  10. NYOJ题目816它合法吗?

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtIAAAJ0CAIAAACwTVMOAAAgAElEQVR4nO3du1LjzNo24O8kyDkQYh