当前位置:网站首页>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
边栏推荐
- 那些关于DOM的常见Hook封装(一)
- EventLoop同步异步,宏任务微任务笔记
- VS2019 compiles boost_1_79, generates 32-bit and 64-bit static libraries
- How to deal with cyber attacks?
- 《基于机器视觉的高压输电线路覆冰厚度检测》论文笔记
- QQ浏览器 replaceAll方法 is not a function 问题解决方法
- SQL注入(4)
- Error detected while processing /home/test/.vim/plugin/visualmark.vim
- What are the functions and applications of the smart counter control board?
- 渗透测试-域环境下的信息收集
猜你喜欢
随机推荐
ffmpeg之H265解码
掌握 TypeToken 原理及泛型擦除
全链路UI设计笔记
JSP入门
网路编程_socket返回值
机器学习入门
【问题记录】pip 安装报错 Failed to establish a new connection
qt字符串之 QString详解
driftingblues靶机wp
Cholesterol-PEG-Maleimide,CLS-PEG-MAL,胆固醇-聚乙二醇-马来酰亚胺用于科研实验
1.02亿美元从数字资产基金撤出!BTC价格已经触底!预示下跌趋势即将逆转?
2022微服务面试题 最新50道题(含答案解析)
SQL注入(3)
笔记本重装系统如何找回之前自己自带的office
MKNetworkKit更换域名时错误解决方法
关于微软2022/2023秋招内推的几句
Deep learning - in the recognition, for example, this paper discusses how to preserve the neural network model
Hcip MPLS experiment
Leetcode刷题——148. 排序链表
Kaggle(六)特征衍生技术 特征聚合









