Digital triangle
Given a digital triangle consisting of n rows of numbers, an algorithm is designed to calculate a path from the top to the bottom of the triangle so that the sum of the numbers that the path passes through can be maximized.
Example input
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Example output
30
Forward pushing method
Problem analysis: When solving the path of a digital triangle from top to bottom, each step can move downward to the left or downward to the right.
Solution: Use an array to store the information of a digital triangle, and another array to store the maximum value of the position from top to bottom. There are only two cases of the maximum value of the position. One is the maximum value of the positive position plus the value of the current position, the other is the maximum value of the previous position above the position plus the value of the position. Compare the two cases and choose the maximum value to be the position. The maximum value of the position is assigned.
The code is as follows
#include<iostream> using namespace std; int main() { int a[100][100],dp[100][100];//An array of a is used to receive values in a digital triangle, and a dp array stores the maximum value reaching that location. int i,j,N; cin>>N;//Hierarchy of Input Digital Triangle for(i=0;i<N;i++) for(j=0;j<=i;j++) //Receiving digital triangles cin>>a[i][j]; b[0][0]=a[0][0];//The first value is the maximum of the current position. for(i=1;i<N;i++) { for(j=0;j<=i;j++) { if(j==0) dp[i][j]=dp[i-1][j]+a[i][j]; //j is 0. That is the sum of the first line. else if(i==j) dp[i][j]=b[i-1][j-1]+a[i][j]; //When i=j, it is the sum of the last oblique rows. else { int x=b[i-1][j-1]+a[i][j];//The sum of the maximum value of the position directly above and the value of the current digital triangle. int y=b[i-1][j]+a[i][j];//The sum of the maximum value of the previous position directly above and the value of the digital triangle of the current position. b[i][j]=x>y?x:y;//The maximum of the two values is the maximum of the current position from top to bottom. } } } int max=b[N-1][0];//Assuming that the path value to the first position of the last line is the largest for(i=0;i<N;i++)//Find out the maximum value of the path in each position of the last line. if(max<b[N-1][i]) max=b[N-1][i]; cout<<max<<endl;//Output maximum path value. }
Backstepping method
Solution: When the digital triangle finds the maximum path from the bottom to the top, it moves n rows to the top left or the top right, and finds the maximum path of each position in line n-1. Then it calculates upward in turn. If the array F(n)(n) is the maximum path, the final required result is F(1)(1).
#include<iostream> using namespace std; const int maxn=10001; int r,A[maxn][maxn],F[maxn][maxn]; int MAX(int s,int t) { if(s>t) return s; return t; }//You can also use the max function that comes with it. int main() { int x,y; cout<<"Please enter the level of the triangle:"<<endl; cin>>r; for(x=1;x<=r;x++) for(y=1;y<=x;y++) cin>>A[x][y]; for(x=r;x>0;x--) for(y=1;y<=x;y++) F[x][y]=MAX(F[x+1][y],F[x+1][y+1])+A[x][y]; //Determining the Maximum Path of the Location cout<<"The maximum value of the path is:"<<F[1][1]; return 0; }