当前位置:网站首页>Informatics orsay a tong (1258: 【 9.2 】 digital pyramid)
Informatics orsay a tong (1258: 【 9.2 】 digital pyramid)
2022-08-02 23:34:00 【orange teacher】
1258: [Example 9.2] Digital Pyramid
Time Limit: 1000 ms Memory Limit: 65536 KB
Commits: 20019 Passes: 11518
【Title Description】
Observe the digital pyramid below.Write a program to find a path that ends anywhere from the highest point to the bottom, so that the sum of the paths passing through the numbers is the largest.Each step can go from the current point to the lower left point or to the lower right point.

In the example above, the path from 13 to 8 to 26 to 15 to 24 yields the maximum sum of 86.
[Enter]
The first line contains R (1≤R≤1000), which indicates the number of lines.
Each subsequent row contains an integer for a particular row of this digital pyramid.
All integers supplied are non-negative and no greater than 100.
【Output】
A single line containing the largest possible sum.
【Sample input】
51311 812 7 266 14 15 812 7 13 24 11【Example of output】
86【Analysis】
Method 1: Push forward
Let a[i][j] store the number tower, and f[i][j] record the sum of the path numbers from the starting point to the jth column of the ith layer.
(1) Division into stages.
Stages: Each layer is a stage; there are five stages in the sample.
(2) Determine the state and state variables.
state: each value in the two-dimensional array is the state.Status information is represented by f[i][j].
(2) Determine the decision and write out the state transition equation.
Where does the value of f[i][j] come from?Of course from column j and column j-1 of row i-1 above.Decisions: From Above?Or from top left?, strategy: maximize the path.Therefore:
The state transition equation: ![f[i][j]=a[i][j]+max\left\{\begin{matrix} f[i-1][j-1]\\ f[i-1][j] \end{matrix}\right.](http://img.inotgo.com/imagesLocal/202208/02/202208022000269923_3.gif)
(4) Find boundary conditions.
When pushing forward, the boundary: f[1][1]=a[1][1].Goal: max(f[n][j])
(5) Design and implement programs.

[Reference code 1]
#include #define MAXN 1010int a[MAXN][MAXN]; //Store the tower dataint f[MAXN][MAXN]; //f[i][j] represents the sum of the path numbers from the starting point to the j column of the i layerint max(int x,int y){return x > y ? x : y;}int main(){int i,j,n,ans;scanf("%d",&n);for(i=1;i<=n;i++)for(j=1;j<=i;j++)scanf("%d",&a[i][j]);f[1][1]=a[1][1];for(i=2;i<=n;i++)for(j=1;j<=i;j++)f[i][j]=max(f[i-1][j-1],f[i-1][j])+a[i][j]; //state transition equationans=0;for(i=1;i<=n;i++) //max(f[n][j])ans=max(ans,f[n][i]);printf("%d",ans);return 0;} Method 2: Inverse method
Where does the value of f[i][j] come from when pushing backwards?is from column j and column j+1 of row i+1 below.The state transition equation is:
![f[i,j]=a[i][j]+max\left\{\begin{matrix} f[i+1][j]\\f[i+1][j+1] \end{matrix}\right.](http://img.inotgo.com/imagesLocal/202208/02/202208022000269923_0.gif)
Boundary: f[n][j]=a[n][j].Target: f[1][1].

[Reference code 2]
#include #define MAXN 1010int a[MAXN][MAXN]; //Store the tower dataint f[MAXN][MAXN]; //f[i][j] represents the sum of the path numbers from the starting point to the j column of the i layerint max(int x,int y){return x > y ? x : y;}int main(){int i,j,n;scanf("%d",&n);for(i=1;i<=n;i++)for(j=1;j<=i;j++)scanf("%d",&a[i][j]);for(i=1;i<=n;i++)f[n][i]=a[n][i];for(i=n-1;i>=1;i--)for(j=1;j<=i;j++)f[i][j]=max(f[i+1][j],f[i+1][j+1])+a[i][j]; //state transition equationprintf("%d",f[1][1]); //target f[1][1]return 0;} a>边栏推荐
- 供电系统电气图
- [AnXun cup 2019] easy_web
- Parse the commonly used methods in the List interface that are overridden by subclasses
- 「每周译Go」这次我们来点不一样的!--《How to Code in Go》系列上线
- 【SLAM】DM-VIO(ros版)安装和论文解读
- 【 LeetCode 】 1374. Generate each character string is an odd number
- 信息学奥赛一本通(1256:献给阿尔吉侬的花束)
- Translate My Wonderful | July Moli Translation Program Winners Announced
- 接口测试常用工具及测试方法(入门篇)
- 云平台简介
猜你喜欢

9,共模抑制比一-不受输入信号中共模波动的影响。【如何分析共模CM抑制比。】

TPAMI2022 | TransCL: based on the study the compression of the Transformer, more flexible and more powerful

李沐动手学深度学习V2-BERT预训练和代码实现

Axure9的元件用法

SQL 入门之第一讲——MySQL 8.0.29安装教程(windows 64位)

KDD 2022 | 深度图神经网络中的特征过相关:一个新视角

供电系统电气图

实现fashion_minst服装图像分类

Triacetin是什么化学材料

AI Scientist: Automatically discover hidden state variables of physical systems
随机推荐
PG's SQL execution plan
【21天学习挑战赛】冒泡排序与插入排序
Day35 LeetCode
Triacetin是什么化学材料
Async的线程池使用的哪个?
[21 Days Learning Challenge] Bubble Sort and Insertion Sort
数字孪生助力智慧城市可视化建设
TPAMI2022 | TransCL: based on the study the compression of the Transformer, more flexible and more powerful
golang源码分析:time/rate
用了TCP协议,就一定不会丢包吗?
Parse the commonly used methods in the List interface that are overridden by subclasses
PLC工作原理动画
LeetCode:622. 设计循环队列【模拟循环队列】
基于 outline 实现头像剪裁以及预览
postgresql autovaccum自动清理
「 每日一练,快乐水题 」1374. 生成每种字符都是奇数个的字符串
9,共模抑制比一-不受输入信号中共模波动的影响。【如何分析共模CM抑制比。】
Solve the docker mysql can't write Chinese
有效解决MySQL报错:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO/YES)
信息学奥赛一本通(1260:【例9.4】拦截导弹(Noip1999))