📌  相关文章
📜  最大化从给定矩阵的左上角单元格到所有其他单元格的路径总和

📅  最后修改于: 2021-04-30 02:25:16             🧑  作者: Mango

给定尺寸为N * M的矩阵mat [] [] ,任务是找到从给定矩阵的左上角单元(0,0)到所有其他单元的最大路径总和。 (i,j)(i,j + 1)只能从任何像元(i,j)移动。

例子:

方法:可以使用动态编程解决问题。以下是解决该问题的递归关系。

请按照以下步骤解决问题:

  1. 初始化矩阵dp [] [] ,其中dp [i] [j]存储从(0,0)(i,j)的最大路径和。
  2. 使用上述递归关系来计算dp [i] [j]的值
  3. 最后,打印dp [] []矩阵的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
#define SZ 100
 
// Function to get the maximum path
// sum from top-left cell to all
// other cells of the given matrix
void pathSum(const int mat[SZ][SZ],
             int N, int M)
{
 
    // Store the maximum path sum
    int dp[N][M];
 
    // Initialize the value
    // of dp[i][j] to 0.
    memset(dp, 0, sizeof(dp));
 
    // Base case
    dp[0][0] = mat[0][0];
    for (int i = 1; i < N; i++) {
        dp[i][0] = mat[i][0]
                   + dp[i - 1][0];
    }
 
    for (int j = 1; j < M; j++) {
        dp[0][j] = mat[0][j]
                   + dp[0][j - 1];
    }
 
    // Compute the value of dp[i][j]
    // using the recurrence relation
    for (int i = 1; i < N; i++) {
        for (int j = 1; j < M; j++) {
            dp[i][j] = mat[i][j]
                       + max(dp[i - 1][j],
                             dp[i][j - 1]);
        }
    }
 
    // Print maximum path sum from
    // the top-left cell
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cout << dp[i][j] << " ";
        }
        cout << endl;
    }
}
 
// Driver Code
int main()
{
    int mat[SZ][SZ]
        = { { 3, 2, 1 },
            { 6, 5, 4 },
            { 7, 8, 9 } };
    int N = 3;
    int M = 3;
 
    pathSum(mat, N, M);
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
 
static final int SZ = 100;
 
// Function to get the maximum path
// sum from top-left cell to all
// other cells of the given matrix
static void pathSum(int [][]mat,
                    int N, int M)
{
  // Store the maximum path sum
  int [][]dp = new int[N][M];
   
  // Base case
  dp[0][0] = mat[0][0];
   
  for (int i = 1; i < N; i++)
  {
    dp[i][0] = mat[i][0] +
               dp[i - 1][0];
  }
 
  for (int j = 1; j < M; j++)
  {
    dp[0][j] = mat[0][j] +
               dp[0][j - 1];
  }
 
  // Compute the value of dp[i][j]
  // using the recurrence relation
  for (int i = 1; i < N; i++)
  {
    for (int j = 1; j < M; j++)
    {
      dp[i][j] = mat[i][j] +
                 Math.max(dp[i - 1][j],
                          dp[i][j - 1]);
    }
  }
 
  // Print maximum path sum from
  // the top-left cell
  for (int i = 0; i < N; i++)
  {
    for (int j = 0; j < M; j++)
    {
      System.out.print(dp[i][j] + " ");
    }
    System.out.println();
  }
}
 
// Driver Code
public static void main(String[] args)
{
  int mat[][] = {{3, 2, 1},
                 {6, 5, 4},
                 {7, 8, 9}};
  int N = 3;
  int M = 3;
  pathSum(mat, N, M);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 Program to implement
# the above appraoch
 
# Function to get the maximum path
# sum from top-left cell to all
# other cells of the given matrix
def pathSum(mat, N, M):
 
    # Store the maximum path sum
    # Initialize the value
    # of dp[i][j] to 0.
    dp = [[0 for x in range(M)]
             for y in range(N)]
 
    # Base case
    dp[0][0] = mat[0][0]
    for i in range(1, N):
        dp[i][0] = (mat[i][0] +
                    dp[i - 1][0])
 
    for j in range(1, M):
        dp[0][j] = (mat[0][j] +
                    dp[0][j - 1])
 
    # Compute the value of dp[i][j]
    # using the recurrence relation
    for i in range(1, N):
        for j in range(1, M):
            dp[i][j] = (mat[i][j] +
                        max(dp[i - 1][j],
                            dp[i][j - 1]))
 
    # Print maximum path sum
    # from the top-left cell
    for i in range(N):
        for j in range(M):
            print(dp[i][j],
                  end = " ")
        print()
 
# Driver code
if __name__ == '__main__':
 
    mat = [[3, 2, 1],
           [6, 5, 4],
           [7, 8, 9]]
    N = 3
    M = 3
    pathSum(mat, N, M)
 
# This code is contributed by Shivam Singh


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
static readonly int SZ = 100;
 
// Function to get the maximum path
// sum from top-left cell to all
// other cells of the given matrix
static void pathSum(int [,]mat,
                    int N, int M)
{
  // Store the maximum path
  // sum
  int [,]dp = new int[N, M];
   
  // Base case
  dp[0, 0] = mat[0, 0];
   
  for (int i = 1; i < N; i++)
  {
    dp[i, 0] = mat[i, 0] +
               dp[i - 1, 0];
  }
 
  for (int j = 1; j < M; j++)
  {
    dp[0, j] = mat[0, j] +
               dp[0, j - 1];
  }
 
  // Compute the value of dp[i,j]
  // using the recurrence relation
  for (int i = 1; i < N; i++)
  {
    for (int j = 1; j < M; j++)
    {
      dp[i, j] = mat[i,j] +
                Math.Max(dp[i - 1, j],
                          dp[i, j - 1]);
    }
  }
 
  // Print maximum path sum from
  // the top-left cell
  for (int i = 0; i < N; i++)
  {
    for (int j = 0; j < M; j++)
    {
      Console.Write(dp[i, j] + " ");
    }
    Console.WriteLine();
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  int [,]mat = {{3, 2, 1},
                {6, 5, 4},
                {7, 8, 9}};
  int N = 3;
  int M = 3;
  pathSum(mat, N, M);
}
}
 
// This code is contributed by 29AjayKumar


输出:
3 5 6 
9 14 18 
16 24 33









时间复杂度: O(N * M)
辅助空间: O(N * M)