Maximum path sum problem for matrices

Description of the problem : There is an m×n matrix, now you want to go from the upper left corner to the lower right corner, and the direction can only be down or right,

It is now stipulated that the weight of a path is the sum of the values ​​passed through this path. Given a matrix, please find the one with the largest weight

path.

Example:
2 5 6 4
3 9 4 3
7 9 1 7
The found path is 2->5->9->9->1->7, and the maximum path sum is 33.

There are three ways of thinking about this problem, one is direct recursive solution, the other is for loop, and the other is dynamic programming

1. Recursive solution

If you want to calculate the maximum path to the lower right corner, you must first calculate the maximum path to the upper point and the maximum path to the left point, and then add 7 to the larger value (the value of the lower right corner of the matrix in the above "problem description" is 7)

When finding the maximum path to the above point, you must first find the maximum path to 1 or 3...and so on until the value in the upper left corner.

#include<stdio.h>
#include<stdlib.h>
#define M 10
int Map[M][M];

int scan_f(int m,int n)
{
    
    
    int i,j;
    for(i=0;i<m;i++)
    {
    
    
        for(j=0;j<n;j++)
        {
    
    
            scanf("%d", &Map[i][j]);
        }
    }
}

int Max(int num1, int num2)
{
    
    
    if(num1 > num2)
        return num1;
    return num2;
}

int Find(int m,int n)
{
    
    
    if(m == 0 && n==0)
        return Map[m][n];
    if(m == 0)
        return Map[m][n] + Find(m, n-1);
    if(n==0)
        return Map[m][n] + Find(m-1, n);
    return Max(Map[m][n]+Find(m-1,n), Map[m][n]+Find(m,n-1));
}

int main()
{
    
    
    int m,n;
    int result;
    scanf("%d %d", &m, &n);
    scan_f(m,n);
    result = Find(m,n);
    printf("%d\n", result);
    return 0;
}

2. for loop

Example: Matrix D is the input matrix

Matrix P is the maximum sum of going to each grid
insert image description here

The P matrix first sets the uppermost row and the leftmost row as the maximum sum of each grid: it is implemented in the first two for loops of the max_path() function in the following code.

Then set the other grids as the maximum sum of each grid: realize in the double for loop of the max_path() function in the following code.

#include<stdio.h>
int P[20][20],D[20][20];
int max(int a,int b)
{
    
    
    if(a>b)
        return a;
    else
        return b;
}
int max_path(int m, int n){
    
    
	P[0][0] = D[0][0];
	for(int i = 1; i < m; i++){
    
    
		P[i][0] = D[i][0] +P[i-1][0];
	}
	for(int i = 1; i < n; i++){
    
    
		P[0][i] = D[0][i] + P[0][i-1];
	}
	for(int i = 1; i < m; i++){
    
    
		for(int j =1; j < n; j++){
    
    
			P[i][j] = D[i][j] + max(P[i-1][j], P[i][j-1]);
        }
    }
    return P[m-1][n-1];
}
int main()
{
    
    
    int m,n;
    scanf("%d %d",&m,&n);
    for(int i=0;i<m;i++)
        for(int j=0;j<n;j++)
            scanf("%d",&D[i][j]);
    int result = max_path(m,n);
    printf("%d",result);
}

3. Dynamic programming
To be continued. . .

Guess you like

Origin blog.csdn.net/qq_43467892/article/details/110450768