码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 递推和记忆化搜索--The Triangle--poj1163


    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

    递推代码

    #include 
    #include 
    #include 
    
    int maxSum(const std::vector<std::vector<int>>& triangle) {
       
        if (triangle.empty()) return 0;
    
        // 创建一个三角形的拷贝,以便我们可以在其中进行计算
        std::vector<std::vector<int>> dp = triangle;
    
        // 从倒数第二行开始往上。因为最后一行的数字已经代表了从那里出发到底部的最大路径和。
        for (int i = dp.size() - 2; i >= 0; --i) {
       
            for (int j = 0; j < dp[i].size(); ++j) {
       
                // 将当前数字加上其下方的两个数字中的较大者
                dp[i][j] 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
  • 相关阅读:
    如何建立团队知识库管理系统,把分散信息有效整理?
    第2-1-3章 docker-compose安装FastDFS,实现文件存储服务
    抽象类和抽象方法
    【luogu CF487E】Tourists(圆方树)(树链剖分)(线段树)
    mysql数据库表之间关系,一对一、一对多、多对多,多表查询
    golang性能分析工具pprof介绍
    深入了解 XXE 注射
    java开发常见面试问题
    30、三维表面重建-Convolutional Occupancy Network
    USDR脱锚事件:稳定币碰上房地产,双重buff想不崩都难!
  • 原文地址:https://blog.csdn.net/yalipf/article/details/133533538
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号