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的 ...
随机推荐
- 常用函数-String
/************************************************************************ 函数功能:将字符串中str的old_value子字符 ...
- docker-以安装软件的方式介绍docker部分命令的使用
[root@ipha-dev71- docker]# docker search python # 搜索镜像 [root@ipha-dev71- docker]# docker pull centos ...
- Nginx初学(一)安装与运行
1.下载nginx安装包 nginx-1.6.3.tar.gz并复制安装包到 /usr/local/software中 2.安装依赖包,命令如下: yum -y install gcc-c++(需要等 ...
- C# 表达式树 Expression
表达式树是定义代码的数据结构. 它们基于编译器用于分析代码和生成已编译输出的相同结构. 几种常见的表达式 BinaryExpression 包含二元运算符的表达式 BinaryExpression b ...
- next day
#_*_coding:utf_*_#!/usr/bin/env python now_day=input('当前某一天:').format('%Y.%m.%d')%%输入日期(年.月.日) year= ...
- Python 常见异常类型
python标准异常 异常名称 描述 BaseException 所有异常的基类Sy ...
- calendar类-时间处理类
calendar类 calendar类是时间处理类 比如在scala中 //字符串转化日期格式 val df = new SimpleDateFormat("yyyy-MM-dd hh:mm ...
- Unity常用协程功能封装
# 1.前言unity开发过程中,经常用到一些特定的协程功能,比如延时触发.等待触发.重复操作等.unity自带Invoke以及InvokeRepeating方法,但这些方法均采用反射机制,性能消耗, ...
- django-表单之模型表单(三)
models.py-->forms.py-->views.py(get)--index.html-->views.py(post)-->home.html urls.py fr ...
- 小白学 Python(14):基础数据结构(集合)(上)
人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...