当前位置:网站首页>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:
(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:
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>边栏推荐
- 软件测试的流程规范有哪些?具体要怎么做?
- 信息学奥赛一本通(1257:Knight Moves)
- The so-called fighting skill again gao also afraid of the chopper - partition, depots, table, and the merits of the distributed
- C语言中变量在内存中的保存与访问
- 「每周译Go」这次我们来点不一样的!--《How to Code in Go》系列上线
- 六石管理学:入门机会只有一次,先把产品做好
- Xcode13.1运行工程报错fatal error: ‘IFlyMSC/IFly.h‘ file not found的问题
- 有效解决MySQL报错:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO/YES)
- 网络协议介绍
- C# Monitor类
猜你喜欢
callback prototype __proto__
Meta 与苹果的元宇宙碰撞
OP analysis and design
第七章 噪声
SQL 嵌套 N 层太长太难写怎么办?
一次线上事故,我顿悟了异步的精髓
Thread线程类基本使用(下)
The time series database has been developed for 5 years. What problem does it need to solve?
"A daily practice, happy water problem" 1374. Generate a string with an odd number of each character
对话亚洲高校首个博士论文奖-裘捷中丨KDD2022
随机推荐
"Weekly Translate Go" This time we have something different!-- "How to Code in Go" series launched
第七章 噪声
即时通讯开发移动端网络短连接的优化手段
「每周译Go」这次我们来点不一样的!--《How to Code in Go》系列上线
The five classification of software testing
基于 flex 布局实现的三栏布局
软件测试的流程规范有哪些?具体要怎么做?
go——垃圾回收机制(GC)
引用类型 ,值类型 ,小坑。
C语言中变量在内存中的保存与访问
SQL Server数据类型转换函数cast()和convert()详解
[21 Days Learning Challenge] Bubble Sort and Insertion Sort
TodoList案例
Xcode13.1运行工程报错fatal error: ‘IFlyMSC/IFly.h‘ file not found的问题
接口测试常用工具及测试方法(入门篇)
LM小型可编程控制器软件(基于CoDeSys)笔记二十五:plc的数据存储区(数字量输入通道部分)
【21天学习挑战赛】冒泡排序与插入排序
Golang source code analysis: time/rate
10 种最佳 IDE 软件 ,你更忠爱哪一个?
网上那么多教人赚钱的方法,但是你实际上是靠什么赚钱的呢?