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的 ...
随机推荐
- PHP stream_wrapper_register
<?php /** * 引用:http://php.net/manual/en/function.stream-wrapper-register.php * 把变量当成文件读写的协议 * * C ...
- css 文字间距
letter-spacing : 字与字之间的距离 text-indent : 行的抬头间距 line-height : 行高度
- PHP 插入排序 -- 折半查找
1. 折半查找 -- Binary Insertion Sort 时间复杂度 : O(n^2) 适用条件 : 相对直接插入排序,减少了数值的比较次数.适用于需要排序的数码比较少的情况. <?p ...
- python方法是什么?
python方法是什么? 方法用来描述对象所具有的行为. 在类中定义的方法可以粗略分为四大类:公有方法.私有方法.静态方法.类方法. 公有方法.私有方法一般所指属于对象的实例方法, 私有方法的名字以两 ...
- python3.8安装flask出现错误“ModuleNotFoundError: No module named '_ctypes'”
本想在CentOS下配置flask+nginx+uwsgi环境,结果安装最基础的flask包都出了问题...以下是我的环境: 服务器:阿里云ECS CentOS7 python版本:3.8.0 问题描 ...
- UITabView
UITabView可是实现列表功能,此文转自https://www.cnblogs.com/longiang7510/p/5367080.html,讲述很详细,都有注视,但是注释解释不太确切,可以自行 ...
- unity message
再用unity进行开发过程中,不可避免的用到消息的传递问题,以下介绍几种消息传递的方法: (一)拖动赋值 此方法即为最普通的方法,即把需要引用的游戏物体或者需要引用的组件拖动到相关公有变量的槽上,然后 ...
- 2018.8.7 python3 for循环中的else语句
for else 简述 用break关键字终止当前循环就不会执行当前的else语句,而使用continue关键字快速进入下一论循环,或者没有使用其他关键字,循环的正常结束后,就会触发else ...
- 身份证号码验证算法(php和js实现)
原文:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=21126994&id=3938244 http://www.jb ...
- [springboot 开发单体web shop] 2. Mybatis Generator 生成common mapper
Mybatis Generator tool 在我们开启一个新项目的研发后,通常要编写很多的entity/pojo/dto/mapper/dao..., 大多研发兄弟们都会抱怨,为什么我要重复写CRU ...