• USACO Training 数字三角形


    USACO Training 数字三角形

    题目描述

    给定一个如下图所示的数字三角形,从顶部出发,在每一结点可以选择移动至其左下方的结点或移动至其右下方的结点,一直走到底层,要求找出一条路径,使路径上的数字的和最大。

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

    输入格式

    第一行包含整数n,表示数字三角形的层数。

    接下来n行,每行包含若干整数,其中第 i 行表示数字三角形第 i 层包含的整数。

    输出格式

    输出一个整数,表示最大的路径数字和。

    数据范围

    1≤n≤500,
    −10000≤三角形中的整数≤10000

    输入样例

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

    输出样例

    30
    

    题目思路

    从下往上将左右分支中较大的记录下来。

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    
    using namespace std;
    
    const int N = 5e2+10;
    int n,f[N][N];
    
    int main()
    {
        cin >> n;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=i;j++)
                scanf("%d",&f[i][j]);
                
        for(int i=n-1;i;i--)
            for(int j=1;j<=i;j++)
                f[i][j] += max(f[i+1][j],f[i+1][j+1]);
        
        cout << f[1][1];
        return 0;
    }
    
  • 相关阅读:
    java设计模式----代理模式
    其他技术----nginx开光
    Less的使用
    C++ 引用和指针
    leetcode 220 Contains Duplicate
    python网络数据采集1
    404
    前端知识点
    tcl自动生成fifo empty checker
    漫话:如何给女朋友解释什么是"大案牍术"?
  • 原文地址:https://www.cnblogs.com/fsh001/p/14271716.html
Copyright © 2020-2023  润新知