当前位置:网站首页>One Pass 1258 - Digital Pyramid (Dynamic Programming)
One Pass 1258 - Digital Pyramid (Dynamic Programming)
2022-08-09 03:25:00 【Bamboo Forest Lay-】
Title
Original title link
http://ybt.ssoier.cn:8088/problem_show.php?pid=1258[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 largest sum of 86.
[Enter]
The first line contains R (1≤R≤1000), indicating the number of lines.
Each subsequent row contains an integer that is contained in a particular row of this digital pyramid.
All integers supplied are non-negative and not 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 output]
86
Thoughts
This problem needs to be done with dynamic programming, not greedy
You can choose the largest of the two branches below each time.This can be done with the help of depth-first search.
Directly using depth-first search will time out, so you need to add memoization
Steps
1. Define variables
const int N=1010;int a[N][N],mk[N][N];int n;2. Define the deep search function
int dfs(int x,int y){if(x>n) return 0;if(mk[x][y]!=-1) return mk[x][y];return mk[x][y]=max(dfs(x+1,y),dfs(x+1,y+1))+a[x][y];}3. Main program
int main(){cin>>n;for(int i=1;i<=n;i++){for(int j=1;j<=i;j++){cin>>a[i][j];}}memset(mk,-1,sizeof(mk));cout<Complete code
#includeusing namespace std;const int N=1010;int a[N][N],mk[N][N];int n;int dfs(int x,int y){if(x>n) return 0;if(mk[x][y]!=-1) return mk[x][y];return mk[x][y]=max(dfs(x+1,y),dfs(x+1,y+1))+a[x][y];}int main(){cin>>n;for(int i=1;i<=n;i++){for(int j=1;j<=i;j++){cin>>a[i][j];}}memset(mk,-1,sizeof(mk));cout< Novice, please give me more advice
边栏推荐
- Deep learning - in the recognition, for example, this paper discusses how to preserve the neural network model
- Cyanine5tetrazine(CAS号:1427705-31-4)结构式原理
- 渗透测试-域环境下的信息收集
- What are the functions and applications of the smart counter control board?
- SQL注入(3)
- 条件变量condition_variable实现线程同步
- 5823. 字符串转化后的各位数字之和
- 新型双功能螯合剂NOTA及其衍生物CAS号:147597-66-8p-SCN-Bn-NOTA
- 5.索引优化实战
- Chrome的JSON美化插件
猜你喜欢
随机推荐
宝塔实测-TinkPHP5.1框架小程序商城源码
【meet host】
开发工程师必备————【Day05】UDP协议;进程的并发与并行
flatMap() :对每个元素执行映射函数并将结果展平
Chapter3 numpy创建数组
JSON beautification plugin for Chrome
深度学习:优化器
leetcode 5709. 最大升序子数组和
【问题记录】pip 安装报错 Failed to establish a new connection
交换VLAN实验
JS ES5也可以创建常量?
对线面试官实现去重和幂等
phpStdudy的下载和DVWA的搭建
A separate machine is connected to the spark cluster of cdh, and the task is submitted remotely (absolutely successful, I have tested it n times)
项目管理-挣值分析方法学习总结
深度学习——以天气识别为例,探讨如何保存神经网络模型
SQL JOIN上的and
Promoting practice with competitions-Like the 84th biweekly game reflection and the 305th weekly game supplementary questions
hcip MPLS 实验
Second data CEO CAI data warming invited to jointly organize the acceleration data elements online salon


![Embedded system driver advanced [2] - platform bus driver development _ basic framework](/img/1c/f9881e6ecdcd8175e88f044c0552e5.jpg)






