18-The Triangle

内存限制:64MB
时间限制:1000ms
Special Judge: No

accepted:5
submit:5

题目描述:

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

分析:
  ①、因为肯定包含第一个数A[1][1],所以为了便于理解,我们不妨从下往上思考
  ②、及就是第n-1我们就确定出对应的n-1个的最大值情况
  ③、状态方程:A[i][j] += max(A[i+1][j], A[i+1][j+1])
步骤:
  ①、从倒数第二层向上依次遍历
  ②、每一层根据状态方程算出该层每一个值对应向下可以得到的最大值
  ③、A[1][1]即为所求 核心代码:
  
 for(int i = n-; i>=; -- i)
for(int j = ; j <= i; ++ j)
A[i][j] += max(A[i+][j], A[i+][j+]);
printf("%d\n", A[][]);

C/C++代码实现(AC):

 #include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <queue>
#include <set>
#include <map>
#include <stack> using namespace std;
const int MAXN = ;
int A[MAXN][MAXN]; int main ()
{
int n;
scanf("%d", &n);
for(int i = ; i <= n; ++ i)
for (int j = ; j <= i; ++ j)
scanf("%d", &A[i][j]); for(int i = n-; i >= ; -- i)
{
for (int j = ; j <= i; ++ j)
{
A[i][j] = max(A[i + ][j], A[i + ][j + ]) + A[i][j];
}
}
printf("%d\n", A[][]);
return ;
}

nyoj 18-The Triangle(动态规划)的更多相关文章

  1. NYOJ 18 The Triangle 填表法,普通dp

    题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=18 The Triangle 时间限制:1000 ms  |  内存限制:6553 ...

  2. nyoj 18 The Triangle

    The Triangle 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure ...

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

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

  4. 120. Triangle(动态规划 三角形最小路径 难 想)

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

  5. Poj1163 The Triangle(动态规划求最大权值的路径)

    一.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 pro ...

  6. poj1163The 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. nyoj 252 01串 动态规划( java)

    当n=2时, 输出 3:当n=3时, 输出 5:当n=4时, 输出 8: #### 分析: 当n=4时,如 0101 符合条件, 当第一个位置是0时,还剩3个位置 ,与n=3时个数相等: 符合条件的为 ...

  8. nyoj 79 拦截导弹 (动态规划)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=79 题意即求最长单调递减子序列 #include<iostream> #inc ...

  9. nyoj 311-完全背包 (动态规划, 完全背包)

    311-完全背包 内存限制:64MB 时间限制:4000ms Special Judge: No accepted:5 submit:7 题目描述: 直接说题意,完全背包定义有N种物品和一个容量为V的 ...

随机推荐

  1. Python之装饰器(二)

    以前你有没有这样一段经历:很久之前你写过一个函数,现在你突然有了个想法就是你想看看,以前那个函数在你数据集上的运行时间是多少,这时候你可以修改之前代码为它加上计时的功能,但是这样的话是不是还要大体读读 ...

  2. Hadoop(MapR)分布式安装及自动化脚本配置

    MapR的分布式集群安装过程还是很艰难的,远远没有计划中的简单.本人总结安装配置,由于集群有很多机器,手动每台配置是很累的,编写了一个自动化配置脚本,下面以脚本为主线叙述(脚本并不完善,后续继续完善中 ...

  3. opencv::sift特征提取

    SIFT特征检测介绍 SIFT(Scale-Invariant Feature Transform)特征检测关键特性: -建立尺度空间,寻找极值 -关键点定位(寻找关键点准确位置与删除弱边缘) -关键 ...

  4. Libevent::evhttp服务器

    #include <cstdio> #include <stdio.h> #include <stdlib.h> #include <string.h> ...

  5. lcx 内网转发

    把放置到已经控制的内网主机 执行 内网主机输入命令lcx.exe -slave 外网ip 外网端口 内网ip 内网端口lcx.exe -slave 30.1.85.55 2222 127.0.0.1 ...

  6. 为什么重写equals必须重写hoshCode的基础分析

    为什么重写equals必须重写hoshCode的基础分析 1.我们先来了解下原生的equals和hashCode代码 原生equals:它判断的是两个对象是否相等 原生hashCode值:它是根据内存 ...

  7. java架构之路-(Redis专题)简单聊聊redis分布式锁

    这次我们来简单说说分布式锁,我记得过去我也过一篇JMM的内存一致性算法,就是说拿到锁的可以继续操作,没拿到的自旋等待. 思路与场景 我们在Zookeeper中提到过分布式锁,这里我们先用redis实现 ...

  8. Java基础(二十一)集合(3)List集合

    一.List接口 List集合为列表类型,列表的主要特征是以线性方式存储对象. 1.实例化List集合 List接口的常用实现类有ArrayList和LinkedList,根据实际需要可以使用两种方式 ...

  9. fenby C语言 P17

    for姐姐 dowhile妹妹 while for(循环变量赋初值,循环条件,循环变量自加) #include <stdio.h> int main(){ int sum=0,i; for ...

  10. SpringCloud Config(配置中心)实现配置自动刷新(十六)

    一.实现原理 1.ConfigServer(配置中心服务端)从远端git拉取配置文件并在本地git一份,ConfigClient(微服务)从ConfigServer端获取自己对应 配置文件: 2.当远 ...