当前位置:网站首页>Dynamic programming - related concepts, (tower problem)
Dynamic programming - related concepts, (tower problem)
2022-07-03 04:59:00 【Madness makes freedom】
Several concepts related to dynamic programming :
- Memory search : For problems with overlapping subproblems , We record the value of the subproblem we encounter for the first time , Use this value directly the next time you encounter the same sub problem , This is also the recursive function to open a two-dimensional array The role of ;
- Overlapping subproblems : A problem can be divided into several sub problems , These sub problems will recur ;
- Optimal substructure : If the optimal solution of a problem can be constructed from the solution of a subproblem ;
- The state has no aftereffect : The current status records historical information , Once the current state is determined , It won't change , And future decisions can only be made on the basis of one or several existing states , Historical information can only affect future decisions through existing states .(PS:《 Algorithm notes 》)
Give a classic question —— The number of towers : A total of several procedures are given , With direct solution output , And the path that gives the maximum value , There are also recursive solutions , There are also recursive solutions :
/**
The number of towers ,
State transition equation :dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+f[i][j];
Optimal substructure : If the optimal solution of a problem can be constructed from the solution of a subproblem ;
1) Output results only ;
data:
5
5
8 3
12 7 16
4 10 11 6
9 5 3 9 4
5
5
8 3
12 7 105
4 10 11 6
78 5 3 9 4
*/
/**
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
cout << " Enter the number of floors of the tower :\n";
cin >> n;
int f[n+1][n+1],dp[n+1][n+1];
printf(" Input %d Value of tower floor \n",n);
for(int i=1;i<=n;++i)
for(int j=1;j<=i;++j)
cin >> f[i][j];
for(int i=1;i<=n;++i)
dp[n][i]=f[n][i];
for(int i=n-1;i>=1;--i)
for(int j=1;j<=i;++j)
{
dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+f[i][j];
}
cout << dp[1][1] << endl;
return 0;
}
*/
Recursive solution :
/** 2) Recursive solution */ /** Memory search : For problems with overlapping subproblems , We record the value of the subproblem we encounter for the first time , The next time you encounter the same subproblem, use this value directly , This is also the recursive function to open a two-dimensional array The role of ; Overlapping subproblems : A problem can be divided into several sub problems , These sub problems will recur ; Optimal substructure : If the optimal solution of a problem can be constructed from the solution of a subproblem ; */ /** #include <iostream> #include <algorithm> #include <cstring> using namespace std; const int maxn=10; int f[maxn][maxn],dp[maxn][maxn]; int n; int max_val(int pos,int index) { memset(*dp,-1,sizeof(dp)); if(pos==n) return f[pos][index]; // If you reach the last layer , Returns the value of this element if(dp[pos][index]!=-1) return dp[pos][index]; else { dp[pos][index]= max(max_val(pos+1,index),max_val(pos+1,index+1))+f[pos][index]; // If you want to select this element , return dp[pos][index]; // Then compare the maximum sum of the two elements next to each other on the next layer and the subsequent selections , Who will vote and who will } } int main() { cout << " Enter the number of floors of the tower :\n"; cin >> n; printf(" Input %d Value of tower floor \n",n); for(int i=1;i<=n;++i) for(int j=1;j<=i;++j) cin >> f[i][j]; cout << max_val(1,1) << endl; return 0; } */
The path to output the maximum value :
/**
3) The path to output the maximum value
*/
/**
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
cout << " The number of floors of the tower :\n";
cin >> n;
int f[n+1][n+1],dp[n+1][n+1];
for(int i=1;i<=n;++i)
for(int j=1;j<=i;++j)
cin >> f[i][j];
for(int i=1;i<=n;++i)
dp[n][i]=f[n][i];
for(int i=n-1;i>=1;--i)
for(int j=1;j<=i;++j)
{
dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+f[i][j];
}
cout << "maximum result:\n" << dp[1][1] << endl;
cout << "path of maxmium:\n" ;
cout << f[1][1];
for(int i=1,j=1;i<n;++i)
{
cout << " -> ";
int temp=dp[i][j]-f[i][j];
if(temp==dp[i+1][j])
cout << f[i+1][j];
else
cout << f[i+1][++j];
}
cout << endl;
return 0;
}
*/
Three dimensional arrays store data , Storage , Intermediate treatment , Output in one
/**
Three dimensional arrays store data , Storage , Intermediate treatment , Output in one
*/
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
cout << " The number of floors of the tower :\n";
cin >> n;
int a[n+1][n+1][3];
for(int i=1;i<=n;++i)
for(int j=1;j<=i;++j)
{
cin >> a[i][j][0];
a[i][j][2]=0; // Suppose each layer selects the left element of the next layer
}
for(int i=1;i<=n;++i)
a[n][i][1]=a[n][i][0];
for(int i=n-1;i>=1;--i)
for(int j=1;j<=i;++j)
{
a[i][j][1]=max(a[i+1][j][1],a[i+1][j+1][1])+a[i][j][0];
if(a[i+1][j][1]<a[i+1][j+1][1])
a[i][j][2]=1; // If the right element of the next layer is better , Is set to 1, Select the right element of the next layer
}
cout << "maximum result:\n" << a[1][1][1] << endl;
cout << "path of maxmium:\n" ;
int i,j;
for( i=1,j=1;i<=n;++i)
{
cout << a[i][j][0];
if(i!=1||i!=n)
cout << " -> ";
j+=a[i][j][2];
}
return 0;
}
边栏推荐
- Prepare for 2022 and welcome the "golden three silver four". The "summary of Android intermediate and advanced interview questions in 2022" is fresh, so that your big factory interview can go smoothly
- [PHP vulnerability weak type] basic knowledge, PHP weak equality, error reporting and bypassing
- Market status and development prospect prediction of the global autonomous hybrid underwater glider industry in 2022
- The least operation of leetcode simple problem makes the array increment
- Problems encountered in fuzzy query of SQL statements
- Introduction to message queuing (MQ)
- M1 Pro install redis
- 1087 all roads lead to Rome (30 points)
- The principle is simple, but I don't know how to use it? Understand "contemporaneous group model" in one article
- The programmer resigned and was sentenced to 10 months for deleting the code. JD came home and said that it took 30000 to restore the database. Netizen: This is really a revenge
猜你喜欢
Network security textual research recommendation
Actual combat 8051 drives 8-bit nixie tube
[tools run SQL blind note]
SSM framework integration
[set theory] relationship properties (symmetry | symmetry examples | symmetry related theorems | antisymmetry | antisymmetry examples | antisymmetry theorems)
论文阅读_ICD编码_MSMN
On typescript and grammar
Silent authorization login and registration of wechat applet
UiPath实战(08) - 选取器(Selector)
Online VR model display - 3D visual display solution
随机推荐
Network security textual research recommendation
[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)
Thesis reading_ Tsinghua Ernie
The programmer resigned and was sentenced to 10 months for deleting the code. JD came home and said that it took 30000 to restore the database. Netizen: This is really a revenge
1107 social clusters (30 points)
Review the configuration of vscode to develop golang
5-36v input automatic voltage rise and fall PD fast charging scheme drawing 30W low-cost chip
Market status and development prospect prediction of the global fire alarm sensor industry in 2022
Thesis reading_ Chinese NLP_ ELECTRA
Flutter monitors volume to realize waveform visualization of audio
[SQL injection point] location and judgment of the injection point
[luatos sensor] 2 air pressure bmp180
Market status and development prospect prediction of global fermented plant protein industry in 2022
【XSS绕过-防护策略】理解防护策略,更好的绕过
I stepped on a foundation pit today
[PHP vulnerability weak type] basic knowledge, PHP weak equality, error reporting and bypassing
Market status and development prospect forecast of global button dropper industry in 2022
1110 complete binary tree (25 points)
Sdl2 + OpenGL glsl practice (Continued)
The usage of micro service project swagger aggregation document shows all micro service addresses in the form of swagger grouping