[Dynamic Programming Question 4] Maximum value of gift&&minimum sum of descent path

The greatest value of a gift

There is a gift placed on each square of an m*n chessboard, and each gift has a certain value (value greater than 0). You can start from the upper left corner of the board to get the gifts in the grid, and move one grid to the right or down at a time until you reach the lower right corner of the board. Given a chessboard and the value of the gifts on it, calculate the maximum value of a gift you can get?

Link: Sword Points Offer 47. The greatest value of a gift

Example 1:
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 12
Explanation: Path 1→3→5→2→1 can get the most value one's gift

1. Status representation

For this "path type" problem, our status representation generally has two forms:

  1. i. Starting from the position [i, j],...;
  2. ii. Starting from the starting position and arriving at the [i, j] position,...;

Here we choose the second way to define the state representation:
dp[i][j] means: reaching the position [i, j], the maximum value at this time.

2. State transition equation

For dp[i][j], we find that there are two ways to reach the position [i, j]:

  1. i. From the position [i - 1, j] above the position [i, j], go one step downward. The value of the gift you can get when you reach the position [i, j] is dp[i - 1][ j] + grid[i][j];
  2. ii. From the position [i, j - 1] to the left of the position [i, j], take one step to the right. At this time, the value of the gift you can get when you reach the position [i, j] is dp[i][j - 1 ] + grid[i][j]

What we want is the maximum value, so the state transition equation is:

dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + grid[i][j] 

3. Initialization

In order to solve some boundary conditions, we can add auxiliary nodes.
In this question, after "adding a row" and "adding a column", all values ​​​​are 0.

4. The order of filling in the form.
According to the "State Transfer Process", the order of filling in the form is "fill in each line from top to bottom" and "fill in each line from left to right".

5. Return value
The value of dp[m][n] should be returned.

Code:

 int maxValue(vector<vector<int>>& grid) {
    
    
        int n=grid.size();
        int m=grid[0].size();
        vector<vector<int>> dp(n+1,vector<int>(m+1));

        for(int i=1;i<=n;i++)
        {
    
    
            for(int j=1;j<=m;j++)
            {
    
    
                dp[i][j]=max(dp[i][j-1],dp[i-1][j])+grid[i-1][j-1];
            }
        }
        return dp[n][m];
    }

Insert image description here

931. Minimum sum of descent path

Given an nxn square integer array matrix, please find and return the minimum sum of the descending path through the matrix.

The descending path can start from any element in the first row and select one element from each row. The element selected in the next row is at most one column away from the element selected in the current row (that is, the first element directly below or diagonally to the left or right). Specifically, the next element at position (row, col) should be (row + 1, col - 1), (row + 1, col) or (row + 1, col + 1).

Link: Minimum Sum of Descending Paths

Insert image description here

Input: matrix = [[2,1,3],[6,5,4],[7,8,9]]
Output: 13
Explanation: As shown in the figure, there are two descending paths with the smallest sum

1. Status representation

For this "path type" problem, our status representation generally has two forms:

  1. i. Starting from the position [i, j],...;
  2. ii. Starting from the starting position and arriving at the [i, j] position,...;

Here we still choose the second way to define state representation:
dp[i][j] means: reaching the position [i, j], the minimum sum of all descending paths.

2. State transition equation

For dp[i][j], we find that there are three ways to reach the position [i, j]:

  1. i. Move from the position directly above [i - 1, j] to the position [i, j];
  2. ii. Move from the upper left position [i - 1, j - 1] to the [i, j] position;
  3. iii. Move from the upper right position [i - 1, j + 1] to the position [i, j];

What we want is the "minimum value" in the three situations, and then add the value of the matrix at the [i, j] position.
then

dp[i][j] = min(dp[i - 1][j], min(dp[i - 1][j - 1], dp[i - 1][j +1])) + matrix[i][j] 

3. Initialization

In order to solve some boundary conditions, we can add auxiliary nodes.
In this question, we need to "add one row" and "add two columns". All positions are initialized to infinity, and then the first row is initialized to 0.

4. Order of filling in the form
The order of filling in the form is from top to bottom

5. Return value
Note that the value of dp[m][n] is not returned here!
The title requires "as long as it reaches the last row", so it should return "the minimum value of the last row in the dp table".

Code:

    int minFallingPathSum(vector<vector<int>>& matrix) {
    
    
        int m=matrix.size();
        int n=matrix[0].size();
        vector<vector<int>> dp(m+1,vector<int> (n+2,INT_MAX));

        for(int i=0;i<n+2;i++) dp[0][i]=0;//初始化第一行的值
        int count=0;
        
        for(int i=1;i<=m;i++)
        {
    
    
            for(int j=1;j<=n;j++)
            {
    
    
                dp[i][j]=min(dp[i-1][j-1],min(dp[i-1][j],dp[i-1][j+1]))+matrix[i-1][j-1];
            }

        }

        int ret=INT_MAX;//返回值
        for(int i=1;i<=n;i++) ret=min(ret,dp[n][i]);
        return ret;
    }

Insert image description here

Guess you like

Origin blog.csdn.net/m0_64579278/article/details/132112128