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项目中登陆超时的功能实现(基于C#)
当我们登陆进网站后,中途去看别的东西,没有再与该网站的服务器交互,就会弹出一个js窗口,登陆超时请重新登陆,并跳转到登陆页面. 步骤1.实现原理,在web.config中配置session的超时时间, ...
- [NOIp2018] luogu P5020 货币系统
还在补暑假作业. 题目描述 你有一个由 NNN 种面值的货币组成的货币系统.定义两个货币系统等价,当且仅当 ∀x∈N∗\forall x\in\N^*∀x∈N∗ 要么同时能被两个货币系统表示,要么同时 ...
- [Luogu2973][USACO10HOL]赶小猪Driving Out the Piggi…
题目描述 The Cows have constructed a randomized stink bomb for the purpose of driving away the Piggies. ...
- Spring Boot项目中使用Mockito
本文首发于个人网站:Spring Boot项目中使用Mockito Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试:生成测试数据初始化数据库用于测试 ...
- 富文本编辑器(wangEditor)
近期在产品的开发工作中遇到要使用富文本编辑器的地方.于是对比了几款编辑器, 最后选择了wangEditor. 优点:轻量.简洁.界面美观.文档齐全. 缺点: 相较于百度ueditor等编辑器功能较 ...
- vue系列---响应式原理实现及Observer源码解析(一)
_ 阅读目录 一. 什么是响应式? 二:如何侦测数据的变化? 2.1 Object.defineProperty() 侦测对象属性值变化 2.2 如何侦测数组的索引值的变化 2.3 如何监听数组内容的 ...
- vue系列---snabbdom.js使用及源码分析(九)
一:什么是snabbdom? 在学习Vue或React中,我们了解最多的就是虚拟DOM,虚拟DOM可以看作是一颗模拟了DOM的Javascript树,主要是通过vnode实现一个无状态的组件,当组件状 ...
- 包管理工具-yum
yum介绍 yum(全称为 Yellow dog Updater, Modified)是一个在 Fedora和 RedHat 以及 CentOS 中的 Shell 前端软件包管理器.基于 RPM 包管 ...
- (一)初识EasyTouch
Easy Touch是一个手指触控(可以鼠标)的插件,可以非常方便的实现各种功能,使用插件第一步是添加Easy Touch组件,可以右键添加也可以在一个空的游戏物体上添加Easy Touch脚本(非事 ...
- UART中的硬件流控RTS与CTS
最近太忙了,没时间写对Ucos-II的移植,先将工作中容易搞错的一个知识点记录下来,关于CTS与RTS的. 在RS232中本来CTS 与RTS 有明确的意义,但自从贺氏(HAYES ) 推出了聪明猫( ...