📌  相关文章
📜  通过从给定矩阵的每个单元格对角遍历来最大化总和

📅  最后修改于: 2021-09-07 02:06:34             🧑  作者: Mango

给定一个维度为N x N的二维方阵arr[][] ,任务是通过从任何单元格对角移动来找到最大路径和,并且每个单元格必须仅访问一次,即从单元格(i, j) ,玩家可以移动到单元格(i + 1, j + 1)

例子:

方法:解决这个问题的思路是对第一行第一列元素对角遍历矩阵,在矩阵范围内求和它们的对角元素。
请按照以下步骤解决问题:

  1. 初始化一个变量,比如max0
  2. 从第一行和第一列中选择每个单元格(i, j)
  3. 现在,从每个单元格中,通过将ij增加1 来找到从该单元格开始的对角线和,例如sum
  4. 然后,将max更新为max(max, sum)
  5. 遍历后,打印max作为所需的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum sum
int MaximumSum(vector >& arr, int n)
{
 
    int ans = 0;
 
    // Loop to traverse through the
    // upper triangular matrix and
    // update the maximum sum to ans
    for (int i = 0; i < n; i++) {
        int x = 0, y = i, sum = 0;
        for (int j = i; j < n; j++) {
            sum += arr[x++][y++];
        }
        if (sum > ans)
            ans = sum;
    }
 
    // Traverse through the
    // lower triangular matrix
    for (int i = 1; i < n; i++) {
 
        int x = i, y = 0, sum = 0;
 
        for (int j = i; j < n; j++) {
 
            sum += arr[x++][y++];
        }
        if (sum > ans)
            ans = sum;
    }
 
    return ans;
}
 
// Driver Code
int main()
{
 
    // Given matrix
    vector > arr;
    arr = { { 1, 2, 3 },
            { 3, 5, 10 },
            { 1, 3, 5 } };
 
    // Given dimension
    int n = arr.size();
 
    cout << MaximumSum(arr, n);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find the maximum sum
static int MaximumSum(int [][]arr, int n)
{
 
    int ans = 0;
 
    // Loop to traverse through the
    // upper triangular matrix and
    // update the maximum sum to ans
    for (int i = 0; i < n; i++)
    {
        int x = 0, y = i, sum = 0;
        for (int j = i; j < n; j++)
        {
            sum += arr[x++][y++];
        }
        if (sum > ans)
            ans = sum;
    }
 
    // Traverse through the
    // lower triangular matrix
    for (int i = 1; i < n; i++)
    {
        int x = i, y = 0, sum = 0;
        for (int j = i; j < n; j++)
        {
            sum += arr[x++][y++];
        }
        if (sum > ans)
            ans = sum;
    }
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
 
    // Given matrix
    int [][]arr = { { 1, 2, 3 },
            { 3, 5, 10 },
            { 1, 3, 5 } };
 
    // Given dimension
    int n = arr.length;
    System.out.print(MaximumSum(arr, n));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to find the maximum sum
def MaximumSum(arr, n):
    ans = 0;
 
    # Loop to traverse through the
    # upper triangular matrix and
    # update the maximum sum to ans
    for i in range(n):
        x, y, sum = 0, i, 0
        for j in range(i, n):
            sum, x, y =sum + arr[x][y], x + 1, y + 1
        if (sum > ans):
            ans = sum
 
    # Traverse through the
    # lower triangular matrix
    for i in range(1, n):
 
        x, y, sum = i, 0, 0
 
        for j in range(i, n):
 
            sum, x, y =sum + arr[x][y], x + 1, y + 1
        if (sum > ans):
            ans = sum
    return ans
 
# Driver Code
if __name__ == '__main__':
 
    # Given matrix
    arr = [ [ 1, 2, 3],
            [ 3, 5, 10],
            [ 1, 3, 5 ]]
 
    # Given dimension
    n = len(arr)
    print (MaximumSum(arr, n))
 
    # This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG{
 
  // Function to find the maximum sum
  static int MaximumSum(int [,]arr, int n)
  {
    int ans = 0;
 
    // Loop to traverse through the
    // upper triangular matrix and
    // update the maximum sum to ans
    for (int i = 0; i < n; i++)
    {
      int x = 0, y = i, sum = 0;
      for (int j = i; j < n; j++)
      {
        sum += arr[x++, y++];
      }
      if (sum > ans)
        ans = sum;
    }
 
    // Traverse through the
    // lower triangular matrix
    for (int i = 1; i < n; i++)
    {
      int x = i, y = 0, sum = 0;
      for (int j = i; j < n; j++)
      {
        sum += arr[x++, y++];
      }
      if (sum > ans)
        ans = sum;
    }
    return ans;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
 
    // Given matrix
    int [,]arr = { { 1, 2, 3 },
                  { 3, 5, 10 },
                  { 1, 3, 5 } };
 
    // Given dimension
    int n = arr.GetLength(0);
    Console.Write(MaximumSum(arr, n));
  }
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
12

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live