The Triangle

动态规划入门经典<一>之数字三角形 一什么是动态规划 二最优化原理 三无后效性 四动态规划的基本思想 1定义最优子问题 2定义状态 3定义决策和状态转移方程 4确定边界条件              The Triangle POJ - 1163 题意 解题思路<一>什么是动态规划? 动态规划是解决多阶段决策问题常用的最优化理论。动态规划适合求解多阶段决策问题的最优解,也可用于含有线性或非线性递推关系的最优解,但是这些问 阅读详情

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 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.
Input

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.
Output

Your program is to write to standard output. The highest sum is written as an integer.
Sample Input

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Sample Output

30

算法思路:
如果把本题看成一个递归的题目其实可以解出答案来,但是解出答案时间太长,几乎说是不科学的,原因就是有大量的没必要计算。2^100是有多少次计算。
所以,我们将换一种思维:动态规划,来解决这个问题,把每次计算出来的结果进行记录下来,这样就解决了不用重复计算的时间,从而我们将我们的无效的计算全部省去。

#include <iostream>
#include <algorithm>

using namespace std;

#define MAX 101
int D[MAX][MAX];
int n;
int maxSum[MAX][MAX];
int MaxSum(int i,int j)
{
    if( maxSum[i][j] != -1)
        return maxSum[i][j];
    if(i == n)
        maxSum[i][j] = D[i][j];
    else{
        int x = MaxSum(i + 1,j);
        int y = MaxSum(i + 1,j + 1);
        maxSum[i][j] = max(x,y) + D[i][j];
    }
    return maxSum[i][j];
}
int main()
{
    int i , j;
    cin >> n;
    for(i = 1; i <= n; i++)
    {
        for(j = 1; j <= i; j++)
        {
            cin >> D[i][j];
            maxSum[i][j] = -1;
        }
    }
    cout << MaxSum(1,1) <<endl;
}

值得学习的代码

//Memory Time 
//232K    0MS 

#include<iostream>
using namespace std;

int max(int a,int b)
{
    return a>b?a:b;
}

int main(int i,int j)
{
    int n;
    while(cin>>n)
    {
        int **way=new int*[n+1];   //动态申请二维数组的第一维,每个元素都是一个一维数组的指针

        /*Input & Initial*/

        for(i=0;i<=n;i++)
        {
            way[i]=new int[i+2]; //动态申请二维数组的第二维,每一行的空间
            for(j=0;j<=i+1;j++)
                way[i][j]=0;   //不能用memset初始化

            if(i!=0)
                for(j=1;j<=i;j++)
                    cin>>way[i][j];
        }

        /*Dp*/

        int max_weight=0;
        for(i=1;i<=n;i++)
            for(j=1;j<=i;j++)
            {
                way[i][j] += max(way[i-1][j-1],way[i-1][j]);

                if(i==n && max_weight<way[i][j])
                    max_weight=way[i][j];
            }

        cout<<max_weight<<endl;

        delete[] way;
    }
    return 0;
}
POJ - 1163:The 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 calculates the... 阅读详情

相关推荐

Aitoearn内容营销的最佳开源 AI 代理,创建 · 发布 · 互动 · 副业一站式平台,支持渠道:抖音、小红书(红点)、微信通道、快手、哔哩哔哩、微信公众号、抖音等等

Aitoearn内容营销的最佳开源 AI 代理,创建 · 发布 · 互动 · 副业一站式平台,支持渠道:抖音、小红书(红点)、微信通道、快手、哔哩哔哩、微信公众号、抖音等等

struggle2025的博客 1749

杭电-oj Palindrome

翻译 Problem Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted

JIAY_WX的博客 331

SAP公司间外协业务流程以及配置说明

SAP公司间外协业务流程以及配置说明

竹大的博客 7161

dp问题 添加最少的字符构成回文串

A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal numb

你若盛开 清风自来 871

深入解析poi-tl自定义表格渲染插件中的多级子循环实现

本文深入解析了poi-tl自定义表格渲染插件中的多级子循环实现,详细介绍了其在处理复杂业务数据时的核心价值、实现原理及实战技巧。通过多级子循环机制,开发者可以高效地将层级数据结构渲染到Word表格中,显著提升代码简洁性和维护效率。文章还分享了性能优化和异常处理经验,帮助开发者更好地应用该插件。

weixin_33734785的博客 483

保姆级教程:在Ubuntu 20.04上从零搞定ROS Noetic和RealSense驱动,为无人机开发铺路

本文提供了一份详细的Ubuntu 20.04环境下ROS Noetic和RealSense驱动安装指南,专为无人机开发环境搭建设计。从系统配置、ROS安装到RealSense驱动集成,涵盖所有关键步骤和常见问题解决方案,帮助开发者快速搭建自主空中机器人开发平台。

weixin_30329623的博客 65

算法课第11周第1题——120. Triangle

题目描述: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2],

bwstardust的博客 329

CANOCO5.0实现冗余分析(RDA)最详细步骤

在地理及生态领域会常使用RDA分析,RDA的实现路径也有很多,今天介绍一下CANOCO软件的实现方法。1.不同样点和环境因子之间夹角是锐角 是正相关,是钝角是负相关。2.夹角越大 说明相关性较小,夹角越小,相关性越大。得有不同的物种或者样点数值,再加上环境因子数据。3.箭头越长 相关性越大。时间调整到2010年。

Along6的博客 1万+

POJ 1163 The Triangle(经典问题教你彻底理解动归思想)

The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38195   Accepted: 22946 Description 7 3 8 8 1 0 2 7 4 4 4 5 2 6 ...

weixin_34405332的博客 155

Windows2008使用-解决漏洞-CVE-2010-2730

随着硬件的升级,操作系统也逐渐进行了版本更新。目前阶段,Windows2016Server、Windows2022Server逐渐成为了以Windows系列的服务器平台系统。对于有些应用而言,由于其业务的固定,可能仍只能运行在Windows2008Server或者Windows2012Server上。有时候我们为安全需要,需要将相关的操作系统涉及的漏洞做修复。最近碰到在一个Windows2008R2 的操作系统漏洞CVE-2010-2730,一顿操作,终于找到了合适的方案,这里分享给大家。

mystonelxj的博客 2313

【AI大模型:架构实战】31、工业级AI大模型系统全景解析:从架构设计到提示语引擎实践

本文系统解析了工业级AI大模型的核心架构与技术体系。整体采用分层解耦设计,包含数据工程、模型训练、模型部署、应用集成和监控运维五大核心模块,以及分布式计算、高速网络、异构硬件三大支撑体系。数据工程构建高质量数据处理流水线;模型训练实现从预训练到业务适配的全流程;模型部署优化服务性能与可用性;应用集成连接业务场景;监控运维保障系统持续优化。三大支撑能力专注于千亿参数管理、高并发低延迟处理和持续学习进化。该架构设计既满足当前需求,又为技术演进预留空间,为企业在数字化转型中提供AI工业化落地的完整解决方案。

2025博客之星Top81。专注AI工程化与架构实战。从分布式思维到模型部署,用工程化视角为你厘清AI落地的真实路径。 1988

nyoj18 The Triangle

题目来源:http://acm.nyist.net/JudgeOnline/problem.php?pid=18 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 1 shows a numb

PandaLHC 491

Cadence PSpice 模型1:基于自带库新建二极管的PSpice模型图文教程

使用Cadence PSpice进行电路图的仿真时,难免会遇到软件自带库中没有合适的元器件的情况,这就需要设计师自己新建元器件模型,本文详细介绍使用PSpice软件修改自带库的元器件模型的参数以得到自己需要的PSpice模型库的方法。

fydar的博客 4413

POJ1163 The Triangle【DP】

The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 58810 Accepted: 35304 Description 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangl...

海岛Blog 750

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

POJ 1163 The Triangle

POJ 1163 The Triangle

爪哇D来客 499

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

[Inheritance]The Triangle Class

The Triangle Class Description Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields named side1, side2, and side3 with default values 1.0 to d...

weixin_43912267的博客 761

NYOJ 18 The Triangle(经典动态规划)

The Triangle时间限制:1000 ms  |  内存限制:65535 KB难度:4描述73 88 1 02 7 4 44 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 s...

MazeFog的博客 365

【ACM菜逼解题报告】The Triangle(POJ1163)

The Triangle(POJ1163) 解题报告,望各路大神海涵、指正,谢谢!

yzxin95的专栏 463

1002.The Triangle

Time Limit: 1sec    Memory Limit:256MB Description Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields named side1, side2, and side3 

1525

(The Triangle class) Design a class named Triangle that extends GeometricObject. The class contains:

**(The Triangle class) Design a class named Triangle that extends GeometricObject. The class contains: ■ Three double data fields named side1, side2, and side3 with default values 1.0 to denote three ...

weixin_43845922的博客 2030
上一篇: 汉诺塔问题----递归
下一篇: C++中for循环的5种语法,值得学习!!!
EncodedStar
博客等级 码龄10年 2614粉丝 162原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值