博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Number Triangles
阅读量:4329 次
发布时间:2019-06-06

本文共 1405 字,大约阅读时间需要 4 分钟。

题目描述

Consider the number triangle shown below. Write a program that calculates the highest sum of numbers that can be 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.
          7
        3   8
      8   1   0
    2   7   4   4
  4   5   2   6   5
In the sample above, the route from 7 to 3 to 8 to 7 to 5 produces the highest sum: 30.
 

 

输入

The first line contains R (1 <= R <= 1000), the number of rows. Each subsequent line contains the integers for that particular row of the triangle. All the supplied integers are non-negative and no larger than 100.

 

输出

A single line containing the largest sum using the traversal specified.

 

样例输入

5

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

样例输出

30

分析:

我就说是个DP题,递什么归递归。

dp[i][j]表示从底层走到达坐标[i][j]的最大值,从底向上扫,状态转移方程为dp[i][j]+=max(dp[i+1][j],dp[i+1][j+1])。

#include 
#include
#include
#include
#include
#include
#include
#include
#define range(i,a,b) for(int i=a;i<=b;++i)#define rerange(i,a,b) for(int i=a;i>=b;--i)#define fill(arr,tmp) memset(arr,tmp,sizeof(arr))using namespace std;int n,dp[1005][1005];void init(){ cin>>n; range(i,1,n)range(j,1,i)cin>>dp[i][j]; rerange(i,n-1,1) range(j,1,i)dp[i][j]+=max(dp[i+1][j],dp[i+1][j+1]);}void solve(){ cout<
<

 

转载于:https://www.cnblogs.com/Rhythm-/p/9324701.html

你可能感兴趣的文章
Android dex分包方案
查看>>
ThreadLocal为什么要用WeakReference
查看>>
删除本地文件
查看>>
FOC实现概述
查看>>
base64编码的图片字节流存入html页面中的显示
查看>>
这个大学时代的博客不在维护了,请移步到我的新博客
查看>>
GUI学习之二十一——QSlider、QScroll、QDial学习总结
查看>>
nginx反向代理docker registry报”blob upload unknown"解决办法
查看>>
gethostbyname与sockaddr_in的完美组合
查看>>
kibana的query string syntax 笔记
查看>>
旋转变换(一)旋转矩阵
查看>>
thinkphp3.2.3 bug集锦
查看>>
[BZOJ 4010] 菜肴制作
查看>>
C# 创建 读取 更新 XML文件
查看>>
KD树
查看>>
VsVim - Shortcut Key (快捷键)
查看>>
C++练习 | 模板与泛式编程练习(1)
查看>>
HDU5447 Good Numbers
查看>>
08.CXF发布WebService(Java项目)
查看>>
java-集合框架
查看>>