【动态规划】The Triangle
问题 E: 【动态规划】The Triangle
时间限制: 1 Sec 内存限制: 128 MB
提交: 24 解决: 24
[提交][状态][讨论版]
题目描述
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.
输入
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.
输出
Your program is to write to standard output. The highest sum is written as an integer.
样例输入
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
样例输出
30
解题思路:首先从上往下是i到n-1,从左往右是j到i;
要从上往下加,先看最上面的7,可以加3等于10,可以加8等于15;
然后看第三行当j等于0,只能加到它右上方那个上,当j=i,只能加到左上方那个数上。
当j!=0或i的时候,可以加到左上方,可以加到右上方,但要求最后的和最大,所以要加到和大的那一个上面。
并且加到大的那一个上这一决策对之后的没有影响,无后效性。
状态转移方程为:sum[i][j]=max(sum[i-1][j],sum[i-1][j-1])+a[i][j];
从上到下,从左到右依次遍历,最后一行其中一个数上会有最大值。
便利最后一行sum[i],找出最大值。
#include <iostream>
#include <cstdio> using namespace std; int main()
{
int a[][];
int sum[][];
int n;
int maxx;
while(scanf("%d",&n)!=EOF){
for(int i=;i<n;i++){
for(int j=;j<i+;j++){
scanf("%d",&a[i][j]);
}
}
sum[][]=a[][];
for(int i=;i<n;i++){
for(int j=;j<i+;j++){
if(j==){
sum[i][j]=sum[i-][j]+a[i][j];
}
if(j==i){
sum[i][j]=sum[i-][i-]+a[i][j];
}
sum[i][j]=max(sum[i-][j],sum[i-][j-])+a[i][j];
}
}
maxx=;
for(int i=;i<n;i++){
if(maxx<sum[n-][i]){
maxx=sum[n-][i];
}
}
printf("%d\n",maxx);
}
return ;
}
【动态规划】The Triangle的更多相关文章
- LeetCode之“动态规划”:Triangle
题目链接 题目要求: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to ...
- LeetCode----Array
Remove Duplicates from Sorted Array 思路:两个指针,头指针在0,尾指针从1开始寻找,找到第一个不等于头指针值的数,覆盖掉头指针后面那个数,然后尾指针往后移. pub ...
- Algorithm Exercises
汇总一些常见的算法题目,参考代码. 注:部分题目没有合适的oj地址 枚举 Perfect Cubes.Biorhythms.Counterfeit Dollar.EXTENDED LIGHTS OUT ...
- POJ 1163 The Triangle(简单动态规划)
http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
- Leetcode OJ : Triangle 动态规划 python solution
Total Accepted: 31557 Total Submissions: 116793 Given a triangle, find the minimum path sum from ...
- The Triangle (简单动态规划)
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 calc ...
- POJ - 1163 The Triangle 【动态规划】
一.题目 The Triangle 二.分析 动态规划入门题. 状态转移方程$$DP[i][j] = A[i][j] + max(DP[i-1][j], DP[i][j])$$ 三.AC代码 1 #i ...
- LeetCode -- Triangle 路径求最小和( 动态规划问题)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Triangle(动态规划)
题目描述 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...
随机推荐
- BIEE 维表
(1) 在物理层给表创建别名(表——>新建对象——>别名) (1) 在业务层创建维度(表——>创建逻辑维——>基于级别层次的维) 钻取是维本身的功能 一 ...
- Java虚拟机类加载机制
看到这个题目,很多人会觉得我写我的java代码,至于类,JVM爱怎么加载就怎么加载,博主有很长一段时间也是这么认为的.随着编程经验的日积月累,越来越感觉到了解虚拟机相关要领的重要性.闲话不多说,老规矩 ...
- IOS基础之 (一) OC基础语法
一 OC语法 1.关键字 基本上所有关键字都是以@开头,比如: @interface , @implementation, @end, @public, @protected, @private 2. ...
- 一个共通的viewModel搞定所有的编辑页面-经典ERP录入页面(easyui + knockoutjs + mvc4.0)
http://www.cnblogs.com/xqin/archive/2013/06/06/3120887.html 前言 我写代码喜欢提取一些共通的东西出来,之前的一篇博客中说了如何用一个共通的v ...
- 安装make命令
步骤1:通过root用户将两个iso源上传到被测试服务器的/opt/huawei/software/iso目录下. # mkdir -p /opt/huawei/software/iso 挂载iso源 ...
- CentOS 6.4下安装Oracle 11gR2
安装前须知: 内存(RAM)的最小要求是 1GB,建议 2GB 及以上. 虚拟内存 swap 建议:内存为 1GB~2GB 时建议swap大小为内存大小的 1.5 倍:内存为 2GB~16GB 时建议 ...
- stl 迭代器(了解)
STL 主要是由 containers(容器),iterators(迭代器)和 algorithms(算法)的 templates(模板)构成的. 对应于它们所支持的操作,共有五种 iterators ...
- chrome 插件开发
写在开头: 相当有意思的UI界面,编码实现,浏览速度.对于一天浏览器使用超过10个小时的人来说,能够定制自己的浏览器,是相当的具有诱惑力. Getting Started 1:目前不支持标准发布版本的 ...
- [LeetCode] Best Time to Buy and Sell Stock III
将Best Time to Buy and Sell Stock的如下思路用到此题目 思路1:第i天买入,能赚到的最大利润是多少呢?就是i + 1 ~ n天中最大的股价减去第i天的. 思路2:第i天买 ...
- spring mvc实现新增用户
spring mvc实现新增用户 1.先在展示页面(查询出来的结果页)添加一个连接<a href="add">添加</a> 2.在后台添加一个添加的方法,点 ...