【动态规划】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 ...
随机推荐
- 【UVALive 3905】BUPT 2015 newbie practice #2 div2-D-3905 - Meteor
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/D The famous Korean internet co ...
- IRP IO_STACK_LOCATION 《寒江独钓》内核学习笔记(1)
在学习内核过滤驱动的过程中,遇到了大量的涉及IRP操作的代码,这里有必要对IRP的数据结构和与之相关的API函数做一下笔记. 1. 相关阅读资料 <深入解析 windows 操作系统(第4版,中 ...
- Scrapy中的item是什么
这两天看Scrapy,看到item这个东西,觉得有点抽象,查了一下,有点明白了. Item 是保存爬取到的数据的容器:其使用方法和python字典类似, 并且提供了额外保护机制来避免拼写错误导致的未定 ...
- 使用存取方法来设置Property value
对比如下代码,第一种使用了存取方法来设置,第二种直接对实例变量操作.显然我们应该采用第一种, 使用第二种情况,简单的情况还好,如果情况一旦复杂,就非常容易出错.并且直接对实例变量操作,不会引发KVO通 ...
- c# 解析JSON的几种办法(转载)
对比 .NET下几种常见的解析JSON方法 主要类 命名空间 限制 内建LINQ支持 DataContractJsonSerializer System.Runtime.Serialization.J ...
- Jquery 获取URL参数
使用jquery获取url以及使用jquery获取url参数是我们经常要用到的操作 1.jquery获取url很简单,代码如下 1.window.location.href; 其实只是用到了javas ...
- Socket网络编程(1)
TCP/IP 简单介绍 应用层 (Application):应用层是个很广泛的概念,有一些基本相同的系统级 TCP/IP 应用以及应用协议,也有许多的企业商业应用和互联网应用. 传输层 (Transp ...
- 学习Linux下的IP地址地理位置信息显示工具nali
首先详细教程已经有前辈做了详细的安装和使用介绍,本人只是参照学习和记录下自己的实际操做. 本文参考档:http://www.cnblogs.com/mchina/archive/2012/12/24/ ...
- (ヒトコト)一个挺有趣的东西!作为一个动漫宅,游戏宅来说还是一个挺有趣的接口!banner上面就是
Hitokoto API 更新:2014.02.22 问题/反馈:api # hitokoto.us 数据获取:[ 数据获取 ] 调用举例:[ JavaScript + HTML (同步) ] [ J ...
- Windows 下 tail 查看日志命令工具分享
以前在公司时服务器上面可以实现tail 命令查看程序运行日志,感觉相当不错,上网查了下这些命令是linux 下的,还好有好心人开发了一个可以在Windows下的运行的小工具,来给分享一下: 使用方法: ...