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

📅  最后修改于: 2021-09-17 16:18:28             🧑  作者: Mango

给定一个矩阵mat[][]的维度N * M ,任务是找到从左上角单元格(0, 0)到给定矩阵的所有其他单元格的最大路径和。任何单元格(i, j) 的唯一可能移动是(i + 1, j)(i, j + 1)

例子:

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

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

  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 approach
 
# 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


Javascript


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

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程