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. Fibonacci 数列和 Lucas 数列的性质、推论及其证明

    Fibonacci 数列 设f(x)=1,x∈{1,2}=f(x−1)+f(x−2),x∈[3,∞)\begin{aligned}f(x)&=1,\quad\quad\quad\quad\qu ...

  2. eclipse中最有用的10个快捷键

    这里列出一些在使用eclipse的过程中最有用的10个快捷键,通过灵活使用这些快捷键可以提高开发效率和开发质量. 1. [ctrl+shift+r]打开资源 这可能是所有快捷键中最省时间的了.这个快捷 ...

  3. spring boot 中通过CORS实现跨域

    一.跨域问题出现的原因 出现跨域问题的原因是同源策略. 同源策略 主要是三同:同协议.同域名.同端口, 同源策略目的 保证用户信息安全,防止恶意网站窃取数据.同源策略是必须的,否则cookie可以共享 ...

  4. 使用maven替换项目依赖中的字节码

    问题描述 我们偶尔会发现一些开源项目的问题,或者出于其他原因,想在某个dependency的代码中加几行或者删除几行来达到目的. 我这里遇到一个dubbo 2.7.3和open feign冲突的问题 ...

  5. C# .NET .NET Framework .NET CORE 等的关系简介

    2019新的一年,祝大家新年快乐,工作生活一帆风顺,心想事成!诸事大吉! 这篇文章是我今年的第一篇博客,主题是:C#  .NET  .NET Framework   .NET CORE  等这些名词之 ...

  6. VS2008给图标工具栏-状态栏添加响应函数

    1.在对话框的:OnInitDialog()函数中添加以下红色代码: BOOL CGSM_MessageDlg::OnInitDialog() { CDialog::OnInitDialog(); / ...

  7. MIT线性代数:9.线性相关,基,维数。

  8. Django学习day7——简单的使用数据库和模型

    Django支持的数据库 PostgreSQL SQLite 3 MySQL Oracle 其中SQLite 3不需要安装,因为SQLite使用文件系统上的独立文件来存储数据 这里我们用SQLite ...

  9. python-->二进制的用法

    1.10进制转换为其他进制 方法一:函数 十进制转二进制:bin(10) --> '0b1010' tpye:是字符串类型 0b:表示2进制 十进制转八进制:oct(10) --> '0o ...

  10. [考试反思]0817NOIP模拟测试24:冲淡

    一切都还好吗? 是啊,还好. 前两名仍然被外校包揽/ B哥140撑住场面,120/110/100不等.我90分混吃等死排了个大并列第10. 考前说要考凸包,打开了几个博客慢慢看一直到考试开始. 然而我 ...