nyoj 18-The Triangle(动态规划)
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(动态规划)的更多相关文章
- NYOJ 18 The Triangle 填表法,普通dp
题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=18 The Triangle 时间限制:1000 ms | 内存限制:6553 ...
- 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 ...
- Leetcode OJ : Triangle 动态规划 python solution
Total Accepted: 31557 Total Submissions: 116793 Given a triangle, find the minimum path sum from ...
- 120. Triangle(动态规划 三角形最小路径 难 想)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 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 ...
- 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 ...
- nyoj 252 01串 动态规划( java)
当n=2时, 输出 3:当n=3时, 输出 5:当n=4时, 输出 8: #### 分析: 当n=4时,如 0101 符合条件, 当第一个位置是0时,还剩3个位置 ,与n=3时个数相等: 符合条件的为 ...
- nyoj 79 拦截导弹 (动态规划)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=79 题意即求最长单调递减子序列 #include<iostream> #inc ...
- nyoj 311-完全背包 (动态规划, 完全背包)
311-完全背包 内存限制:64MB 时间限制:4000ms Special Judge: No accepted:5 submit:7 题目描述: 直接说题意,完全背包定义有N种物品和一个容量为V的 ...
随机推荐
- Web前端安全之iframe
防嵌套网页 比如,最出名的clickhacking就是使用iframe来 拦截click事件.因为iframe享有着click的最优先权,当有人在伪造的主页中进行点击的话,如果点在iframe上,则会 ...
- [Luogu3112] [USACO14DEC]后卫马克Guard Mark
题意翻译 FJ将飞盘抛向身高为H(1 <= H <= 1,000,000,000)的Mark,但是Mark被N(2 <= N <= 20)头牛包围.牛们可以叠成一个牛塔,如果叠 ...
- wait,notify,notifyAll详细介绍
https://www.cnblogs.com/pangyang/articles/5916349.html
- vue —— 监听
vue的监听用途很大 比如:通过数据的值的变化,执行某个方法 首先:data中要有个变量初始值 finalTotalAmount的初始值是0 我们想当finalTotalAmount值发生变化时,执行 ...
- WebStorm 使用过程中出现的一些问题以及解决方案
标签: WebStorm 配置 描述: 有关 WebStorm 使用过程中出现的一些问题以及其解决方案的汇总 "unresolved function or method" 问题描 ...
- OsmocomBB软件实现栈概况
OsmocomBB软件实现栈概况 简单地说,本文仅描述软件中GSM信号接收到部分. 暂不提及发送流程,引导加载/引导流程,以及各种控制路径特别是从layer1到RF硬件. 首先,通过天线接收RF信号, ...
- 使用haproxy实现负载均衡集群
一.HAProxy概述: HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.根据官方数据,其最高极限支持10G的并发. HAP ...
- 2019.NET Conf,我们在共同期待
(一)回顾一个小社区红过的五分钟 不知不觉,距离中国.net社区组织的.net conf只有不到一周的时间,还记得年初在叶伟民老师,潘淳老师和张善友老师的号召下,我们长沙的十几位开发者自发组织起来,拉 ...
- Hibernate 查询方式、JPA查询方式
hibernate 查询方式: OID 查询 对象导航查询 HQL 方式查询 QBC方式查询 原生SQL方式查询 JPA 查询方式: OID 查询 对象导航查询 JPQL 方式查询 CriteriaB ...
- 【xinsir】函数库,持续更新
1.遍历文件-node // 递归遍历目录下的文件 function readDirSync (path) { var pa = fs.readdirSync(path); pa.forEach(fu ...