当前位置:网站首页>Dynamic programming classical topic triangle shortest path
Dynamic programming classical topic triangle shortest path
2022-06-11 00:39:00 【Wutiande, young Xia】
Preface
Give an array of triangles , Find the shortest path
for example :
Ideas
- From the last floor , Build a n+1 Of length dp Array , Represents the shortest path from the current layer to the lowest layer , Initialize all to 0, Dynamic planning from back to front : Current dp The update value is ( Current position value + m i n ( When front position Set up Next One layer 2 individual phase adjacent position Set up Of d p value ) min( The current position is next to 2 Of adjacent locations dp value ) min( When front position Set up Next One layer 2 individual phase adjacent position Set up Of dp value ))
- The green value is the updated value

Sample code
#include<iostream>
#include<vector>
using namespace std;
int main() {
vector<vector<int>> triangle{ {2},{3,4},{6,5,7},{4,1,8,3} };
/*for (auto avec : triangle) {
for (auto num : avec) {
cout << num << " ";
}
cout << endl;
}*/
int length = triangle.size();
//cout << length; // 4
vector<int> dp; //dp Array initialization
for (int i = 0; i < length+1; i++) {
dp.push_back(0);
}
// From bottom to top dp
for (int i = length - 1; i >= 0; i--) {
// From the last floor
for (int j = 0; j < i+1; j++) { // The first i Layer has a i+1 Number
// From 0 Update started at dp Array
// The minimum path value for the current location is : Current value +( The adjacent minimum path value of the next layer of the current layer )
//cout << i << j<<endl;
dp[j] = triangle[i][j] + min(dp[j], dp[j + 1]);
}
}
cout << dp[0] << endl; // Shortest path required
// Print the final dp Array
for (auto item : dp) {
cout << item << " ";
}
return 0;
}
result

边栏推荐
- [no title] 66666
- 百度飞桨PaddlePaddle最新系列AI课程回放地址
- LeetCode 1996. Number of weak characters in the game*
- 非重叠矩形中的随机点
- VTK例子--三个相交的平面
- Unable to return to the default page after page Jump
- Njupt Nanyou Discrete Mathematics_ Experiment 3
- [network planning] 2.5 brief introduction to P2P architecture
- 项目连接不到远程虚拟机The driver has not received any packets from the server.
- Njupt Nanyou Discrete Mathematics_ Experiment 2
猜你喜欢

双飞翼布局
![[network planning] 1.5 seven layer network model and five layer network model](/img/a8/74a1b44ce4d8b0b1a85043a091a91d.jpg)
[network planning] 1.5 seven layer network model and five layer network model

How word inserts a guide (dot before page number) into a table of contents

Pirate OJ 148 String inversion

AQS explanation of concurrent programming

How word removes the header line

微信小程序实现OCR扫描识别

Unity自定义文件夹图标颜色 个性化Unity编译器

Dual wing layout

Unable to return to the default page after page Jump
随机推荐
Exemple VTK - - trois plans qui se croisent
Multipass中文文档-概览
[network planning] 2.4 DNS: directory service of the Internet
[network planning] 2.5 brief introduction to P2P architecture
Njupt South Post collection_ Experiment 2
JVM garbage collection mechanism and common garbage collectors
富文本活动测试1
LeetCode 1996. Number of weak characters in the game*
Excel单元格
数的奥秘之幂数与完全平方数
Unity custom folder icon color personalized unity compiler
字符串时间排序,对时间格式字符串进行排序
Static method static learning
图的最短路径问题 详细分解版
Bluetooth development (3) -- look at the air bag
Qt客户端套接字QTcpSocket通过bind指定本地ip
MESI cache consistency protocol for concurrent programming
How to install mathtype6.9 and related problem solutions in office2016 (word2016)
SQL查询,子查询作为结果字段
ts+fetch实现选择文件上传