📌  相关文章
📜  通过旋转所有行或所有列来最大化矩阵的对角线总和

📅  最后修改于: 2021-04-22 08:24:51             🧑  作者: Mango

给定尺寸为N * N的正方形矩阵mat [] [] ,任务是通过将矩阵的所有行或所有列旋转正整数,从给定的矩阵中找到对角元素的最大和。

例子:

方法:想法是以所有可能的方式旋转矩阵的所有行和列,并计算获得的最大和。请按照以下步骤解决问题:

  • 初始化一个变量,例如maxDiagonalSum,以通过旋转矩阵的所有行或列来存储矩阵对角元素的最大可能和。
  • 将矩阵的所有行旋转[0,N – 1]范围内的正整数,并更新maxDiagonalSum的值。
  • 将矩阵的所有列旋转[0,N – 1]范围内的正整数,并更新maxDiagonalSum的值。
  • 最后,输出maxDiagonalSum的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
#define N 3
 
 
// Function to find maximum sum of diagonal elements
// of matrix by rotating either rows or columns
int findMaximumDiagonalSumOMatrixf(int A[][N])
{
     
     
    // Stores maximum diagonal sum of elements
    // of matrix by rotating rows or columns
    int maxDiagonalSum = INT_MIN;
     
 
    // Rotate all the columns by an integer
    // in the range [0, N - 1]
    for (int i = 0; i < N; i++) {
         
 
        // Stores sum of diagonal elements
        // of the matrix
        int curr = 0;
         
 
        // Calculate sum of diagonal
        // elements of the matrix
        for (int j = 0; j < N; j++) {
             
 
            // Update curr
            curr += A[j][(i + j) % N];
        }
         
         
        // Update maxDiagonalSum
        maxDiagonalSum = max(maxDiagonalSum,
                                      curr);
    }
 
 
    // Rotate all the rows by an integer
    // in the range [0, N - 1]
    for (int i = 0; i < N; i++) {
         
 
        // Stores sum of diagonal elements
        // of the matrix
        int curr = 0;
         
 
        // Calculate sum of diagonal
        // elements of the matrix
        for (int j = 0; j < N; j++) {
             
 
            // Update curr
            curr += A[(i + j) % N][j];
        }
         
         
        // Update maxDiagonalSum
        maxDiagonalSum = max(maxDiagonalSum,
                                      curr);
    }
 
       
    return maxDiagonalSum;
}
 
 
// Driver code
int main()
{
     
    int mat[N][N] = { { 1, 1, 2 },
                    { 2, 1, 2 },
                    { 1, 2, 2 } };
     
    cout<< findMaximumDiagonalSumOMatrixf(mat);
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
static int N = 3;
  
// Function to find maximum sum of
// diagonal elements of matrix by
// rotating either rows or columns
static int findMaximumDiagonalSumOMatrixf(int A[][])
{
     
    // Stores maximum diagonal sum of elements
    // of matrix by rotating rows or columns
    int maxDiagonalSum = Integer.MIN_VALUE;
     
    // Rotate all the columns by an integer
    // in the range [0, N - 1]
    for(int i = 0; i < N; i++)
    {
         
        // Stores sum of diagonal elements
        // of the matrix
        int curr = 0;
         
        // Calculate sum of diagonal
        // elements of the matrix
        for(int j = 0; j < N; j++)
        {
             
            // Update curr
            curr += A[j][(i + j) % N];
        }
          
        // Update maxDiagonalSum
        maxDiagonalSum = Math.max(maxDiagonalSum,
                                  curr);
    }
     
    // Rotate all the rows by an integer
    // in the range [0, N - 1]
    for(int i = 0; i < N; i++)
    {
         
        // Stores sum of diagonal elements
        // of the matrix
        int curr = 0;
         
        // Calculate sum of diagonal
        // elements of the matrix
        for(int j = 0; j < N; j++)
        {
             
            // Update curr
            curr += A[(i + j) % N][j];
        }
         
        // Update maxDiagonalSum
        maxDiagonalSum = Math.max(maxDiagonalSum,
                                  curr);
    }
    return maxDiagonalSum;
}
  
// Driver Code
public static void main(String[] args)
{
    int[][] mat = { { 1, 1, 2 },
                    { 2, 1, 2 },
                    { 1, 2, 2 } };
      
    System.out.println(
        findMaximumDiagonalSumOMatrixf(mat));
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program to implement
# the above approach
import sys
 
N = 3
 
# Function to find maximum sum of diagonal
# elements of matrix by rotating either
# rows or columns
def findMaximumDiagonalSumOMatrixf(A):
     
    # Stores maximum diagonal sum of elements
    # of matrix by rotating rows or columns
    maxDiagonalSum = -sys.maxsize - 1
 
    # Rotate all the columns by an integer
    # in the range [0, N - 1]
    for i in range(N):     
 
        # Stores sum of diagonal elements
        # of the matrix
        curr = 0
         
        # Calculate sum of diagonal
        # elements of the matrix
        for j in range(N):
             
            # Update curr
            curr += A[j][(i + j) % N]
        
        # Update maxDiagonalSum
        maxDiagonalSum = max(maxDiagonalSum,
                             curr)
                              
    # Rotate all the rows by an integer
    # in the range [0, N - 1]
    for i in range(N):
         
        # Stores sum of diagonal elements
        # of the matrix
        curr = 0
         
        # Calculate sum of diagonal
        # elements of the matrix
        for j in range(N):         
             
            # Update curr
            curr += A[(i + j) % N][j]
         
        # Update maxDiagonalSum
        maxDiagonalSum = max(maxDiagonalSum,
                             curr)
                              
    return maxDiagonalSum
 
# Driver code
if __name__ == "__main__":
     
    mat = [ [ 1, 1, 2 ],
            [ 2, 1, 2 ],
            [ 1, 2, 2 ] ]
     
    print(findMaximumDiagonalSumOMatrixf(mat))
     
# This code is contributed by chitranayal


C#
// C# program to implement
// the above approach 
using System;
   
class GFG{
   
static int N = 3;
   
// Function to find maximum sum of
// diagonal elements of matrix by
// rotating either rows or columns
static int findMaximumDiagonalSumOMatrixf(int[,] A)
{
     
    // Stores maximum diagonal sum of elements
    // of matrix by rotating rows or columns
    int maxDiagonalSum = Int32.MinValue;
     
    // Rotate all the columns by an integer
    // in the range [0, N - 1]
    for(int i = 0; i < N; i++)
    {
         
        // Stores sum of diagonal elements
        // of the matrix
        int curr = 0;
          
        // Calculate sum of diagonal
        // elements of the matrix
        for(int j = 0; j < N; j++)
        {
             
            // Update curr
            curr += A[j, (i + j) % N];
        }
         
        // Update maxDiagonalSum
        maxDiagonalSum = Math.Max(maxDiagonalSum,
                                  curr);
    }
      
    // Rotate all the rows by an integer
    // in the range [0, N - 1]
    for(int i = 0; i < N; i++)
    {
         
        // Stores sum of diagonal elements
        // of the matrix
        int curr = 0;
          
        // Calculate sum of diagonal
        // elements of the matrix
        for(int j = 0; j < N; j++)
        {
             
            // Update curr
            curr += A[(i + j) % N, j];
        }
          
        // Update maxDiagonalSum
        maxDiagonalSum = Math.Max(maxDiagonalSum,
                                  curr);
    }
    return maxDiagonalSum;
}
   
// Driver Code
public static void Main()
{
    int[,] mat = { { 1, 1, 2 },
                   { 2, 1, 2 },
                   { 1, 2, 2 } };
       
    Console.Write(findMaximumDiagonalSumOMatrixf(mat));
}
}
 
// This code is contributed by code_hunt


输出:
6

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