当前位置:网站首页>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>边栏推荐
猜你喜欢
开关、电机、断路器、电热偶、电表接线图大全
实现fashion_minst服装图像分类
Thread线程类基本使用(上)
用了TCP协议,就一定不会丢包吗?
SQL 入门之第一讲——MySQL 8.0.29安装教程(windows 64位)
李沐动手学深度学习V2-BERT预训练和代码实现
"A daily practice, happy water problem" 1374. Generate a string with an odd number of each character
ECCV 2022 | 通往数据高效的Transformer目标检测器
unittest自动化测试框架总结
Five data structures of Redis and their corresponding usage scenarios
随机推荐
Flutter 常见异常分析
第七章 噪声
浅议.NET遗留应用改造
Meta 与苹果的元宇宙碰撞
传感器工作原理
EasyExcel dynamic parsing and save table columns
基于“无依赖绝对定位”实现的圣杯三栏布局
Geoip2 - golang golang source code analysis
开关、电机、断路器、电热偶、电表接线图大全
golang 源码分析:juju/ratelimit
供电系统电气图
信息学奥赛一本通(1258:【例9.2】数字金字塔)
一次线上事故,我顿悟了异步的精髓
SQL 入门之第一讲——MySQL 8.0.29安装教程(windows 64位)
The time series database has been developed for 5 years. What problem does it need to solve?
接口测试常用工具及测试方法(入门篇)
Informatics Olympiad All-in-One (1259: [Example 9.3] Find the longest non-descending sequence)
数字孪生助力智慧城市可视化建设
软件测试的流程规范有哪些?具体要怎么做?
「每周译Go」这次我们来点不一样的!--《How to Code in Go》系列上线