问题 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的更多相关文章

  1. LeetCode之“动态规划”:Triangle

    题目链接 题目要求: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to ...

  2. LeetCode----Array

    Remove Duplicates from Sorted Array 思路:两个指针,头指针在0,尾指针从1开始寻找,找到第一个不等于头指针值的数,覆盖掉头指针后面那个数,然后尾指针往后移. pub ...

  3. Algorithm Exercises

    汇总一些常见的算法题目,参考代码. 注:部分题目没有合适的oj地址 枚举 Perfect Cubes.Biorhythms.Counterfeit Dollar.EXTENDED LIGHTS OUT ...

  4. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  5. Leetcode OJ : Triangle 动态规划 python solution

    Total Accepted: 31557 Total Submissions: 116793     Given a triangle, find the minimum path sum from ...

  6. 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 ...

  7. POJ - 1163 The Triangle 【动态规划】

    一.题目 The Triangle 二.分析 动态规划入门题. 状态转移方程$$DP[i][j] = A[i][j] + max(DP[i-1][j], DP[i][j])$$ 三.AC代码 1 #i ...

  8. LeetCode -- Triangle 路径求最小和( 动态规划问题)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  9. Triangle(动态规划)

    题目描述 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...

随机推荐

  1. 【CodeForces 520E】Pluses everywhere

    题意 n个数里插入k个+号,所有式子的和是多少(取模1000000007) (0 ≤ k < n ≤ 105). 分析 1.求答案,考虑每个数作为i位数(可为答案贡献10的i-1次方,个位i=1 ...

  2. Oracle 11g 默认用户名和密码

    安装ORACLE时,若没有为下列用户重设密码,则其默认密码如下: 用户名 / 密码                      登录身份                              说明 ...

  3. shell 命令遇到的一些问题

    1.  command not found 一般都是未安装,需要root 权限去安装服务,就可正常使用.比如rz, sz, crontab, sendemail, lftp等 2. rz 传输失败,输 ...

  4. 装了虚拟机,但是没有虚拟网卡vmnet0 vmnet1 vmnet8

    在服务里面启动图中的两个服务,在再你的虚拟网络编辑器里面点恢复默认设置就会出现了,我的是win8.1,默认这两个服务是手动启动的,可能是优化软件优化的结果 我是win10  里面显示以太网3 和 4, ...

  5. C#通过编程方式实现Ping

    代码是照着书敲的,贴出来方便平时参考 using System; using System.Collections.Generic; using System.Linq; using System.T ...

  6. ORACLE RAC集群的体系结构

    RAC是一个完整的集群应用环境,它不仅实现了集群的功能,而且提供了运行在集群之上的应用程序,即Oracle数据库.无论与普通的集群相比,还是与普通的Oracle数据库相比,RAC都有一些独特之处. R ...

  7. 深入浅出Redis03 使用Redis数据库(hashes类型)

    一 hashes类型及操作 Redis hash是一个string类型的field和value的映射表.它的添加,删除操作都是0(1)(平均).hash特别适合用于存储对象,相较于对将对象的每个字段存 ...

  8. leach和leach-c协议仿真

    http://blog.csdn.net/codingkid/article/details/7215216 1.复制leach_test为leach-c_test,修改里面的文件夹和输出文件名.并且 ...

  9. python numpy 教程

    http://blog.chinaunix.net/uid-21633169-id-4408596.html

  10. photoshop几个基本技巧

    原文地址:http://blog.thmz.com/user1/936/archives/2008/20418.htm 去除文字的几种方法: 1.访印图章工具 2.修补工具 3.修复画笔工具 4.画笔 ...