当前位置:网站首页>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>边栏推荐
猜你喜欢

姑姑:给小学生出点口算题

Qt提升自定义控件,找不到头文件

Tencent YunMeng every jie: I experienced by cloud native authors efficiency best practices case

Parse the commonly used methods in the List interface that are overridden by subclasses

Axure9的元件用法

iframe------------frame-

Leetcode刷题——单调栈问题(739每日温度问题、496下一个更大元素I、503下一个更大元素 II)

Leetcode刷题——字符串相加相关题目(415. 字符串相加、面试题 02.05. 链表求和、2. 两数相加)
Solve the docker mysql can't write Chinese

Common tools and test methods for interface testing (Introduction)
随机推荐
Day12 接口和协议
TPAMI2022 | TransCL: based on the study the compression of the Transformer, more flexible and more powerful
浅议.NET遗留应用改造
PLC工作原理动画
Digital twins help visualize the construction of smart cities
The so-called fighting skill again gao also afraid of the chopper - partition, depots, table, and the merits of the distributed
Triacetin是什么化学材料
信息学奥赛一本通(1260:【例9.4】拦截导弹(Noip1999))
你所不知道的C#中的细节
Leetcode刷题——字符串相加相关题目(415. 字符串相加、面试题 02.05. 链表求和、2. 两数相加)
什么是 IDE
MSTP与STP
模板的进阶
【实战 已完结】WPF开发自动化生产管理平台
软件测试分类
【手撕AHB-APB Bridge】~ AMBA总线 之 APB
Flutter with internationalized adapter automatically generated
Parse the commonly used methods in the List interface that are overridden by subclasses
Informatics Olympiad All-in-One (1260: [Example 9.4] Intercepting Missiles (Noip1999))
Solve the docker mysql can't write Chinese