📌  相关文章
📜  从给定的 2N X 2N 矩阵中最大化 NXN 左上子矩阵的总和

📅  最后修改于: 2022-05-13 01:57:21.615000             🧑  作者: Mango

从给定的 2N X 2N 矩阵中最大化 NXN 左上子矩阵的总和

给定一个2N x 2N的整数矩阵。您可以以任意顺序反转任意行或列任意次数。任务是计算左上角NXN子矩阵的最大和,即从(0, 0) 到(N – 1, N – 1) 的子矩阵元素之和。
例子:

Input : mat[][] = {
                    112, 42, 83, 119,
                    56, 125, 56, 49,
                    15, 78, 101, 43,
                    62, 98, 114, 108
                  }
Output : 414
Given matrix is of size 4 X 4, we need to maximize 
the sum of upper left 2 X 2 matrix i.e 
the sum of mat[0][0] + mat[0][1] + mat[1][0] + mat[1][1].
Following operations maximize the sum:
1. Reverse the column 2,
112, 42, 114, 119,
56, 125, 101, 49,
15, 78, 56, 43,
62, 98, 83, 108

2. Reverse row 0,
119, 114, 42, 112,
56, 125, 101, 49,
15, 78, 56, 43,
62, 98, 83, 108

Sum of upper-left matrix = 119 + 114 + 56 + 125 = 414.

为了使左上子矩阵的和最大化,观察,对于左上子矩阵的每个单元格,有四个候选,即左上角、右上角、左下角和右下角子矩阵中的对应单元格它可以交换。
现在,观察每个单元格,无论它在哪里,我们都可以将其与左上角子矩阵中的相应候选值交换,而不改变左上角子矩阵中其他单元格的顺序。该图显示了一个实例,其中最大值4 个候选者的值在右上角的子矩阵中。如果它在左下或右下子矩阵中,我们可以先反转一行或一列,将其放在右上子矩阵中,然后按照与图中所示相同的操作顺序进行操作。
在这个矩阵中,假设26是 4 个候选者中的最大值,并且23必须与26交换,而不改变左上子矩阵中单元格的顺序。

矩阵

反转第 2 行,

反向第 2 列,

反转第 7 行,

反转第 6 列,

反转第 2 行,

下面是这种方法的实现:

C++
// C++ program to find maximum value of top N/2 x N/2
// matrix using row and column reverse operations
#include
#define R 4
#define C 4
using namespace std;
 
int maxSum(int mat[R][C])
{
  int sum = 0;
 
  for (int i = 0; i < R/2; i++)
    for (int j = 0; j < C/2; j++)
    {
      int r1 = i;
      int r2 = R - i - 1;
      int c1 = j;
      int c2 = C - j - 1;
       
      // We can replace current cell [i, j]
      // with 4 cells without changing affecting
      // other elements.
      sum += max(max(mat[r1][c1], mat[r1][c2]),
                 max(mat[r2][c1], mat[r2][c2]));
    }
     
    return sum;
}
 
// Driven Program
int main()
{
  int mat[R][C] = {
                    112, 42, 83, 119,
                    56, 125, 56, 49,
                    15, 78, 101, 43,
                    62, 98, 114, 108
                  };
 
  cout << maxSum(mat) << endl;
 
  return 0;
}


Java
// Java program to find maximum value of top N/2 x N/2
// matrix using row and column reverse operations
 
class GFG {
 
    static int R = 4;
    static int C = 4;
 
    static int maxSum(int mat[][]) {
        int sum = 0;
 
        for (int i = 0; i < R / 2; i++) {
            for (int j = 0; j < C / 2; j++) {
                int r1 = i;
                int r2 = R - i - 1;
                int c1 = j;
                int c2 = C - j - 1;
 
                // We can replace current cell [i, j]
                // with 4 cells without changing affecting
                // other elements.
                sum += Math.max(Math.max(mat[r1][c1], mat[r1][c2]),
                        Math.max(mat[r2][c1], mat[r2][c2]));
            }
        }
 
        return sum;
    }
 
// Driven Program
    public static void main(String[] args) {
        int mat[][] = {
            {112, 42, 83, 119},
            {56, 125, 56, 49},
            {15, 78, 101, 43},
            {62, 98, 114, 108}
        };
 
        System.out.println(maxSum(mat));
 
    }
}
/* This Java code is contributed by Rajput-Ji*/


Python3
# Python3 program to find the maximum value
# of top N/2 x N/2 matrix using row and
# column reverse operations
def maxSum(mat):
 
    Sum = 0
    for i in range(0, R // 2):
        for j in range(0, C // 2):
         
            r1, r2 = i, R - i - 1
            c1, c2 = j, C - j - 1
                 
            # We can replace current cell [i, j]
            # with 4 cells without changing/affecting
            # other elements.
            Sum += max(max(mat[r1][c1], mat[r1][c2]),
                       max(mat[r2][c1], mat[r2][c2]))
         
    return Sum
 
# Driver Code
if __name__ == "__main__":
 
    R = C = 4
    mat = [[112, 42, 83, 119],
           [56, 125, 56, 49],
           [15, 78, 101, 43],
           [62, 98, 114, 108]]
 
    print(maxSum(mat))
 
# This code is contributed
# by Rituraj Jain


C#
// C# program to find maximum value
// of top N/2 x N/2 matrix using row
// and column reverse operations
using System;
 
class GFG
{
static int R = 4;
static int C = 4;
 
static int maxSum(int[,] mat)
{
    int sum = 0;
 
    for (int i = 0; i < R / 2; i++)
    {
        for (int j = 0; j < C / 2; j++)
        {
            int r1 = i;
            int r2 = R - i - 1;
            int c1 = j;
            int c2 = C - j - 1;
 
            // We can replace current cell [i, j]
            // with 4 cells without changing affecting
            // other elements.
            sum += Math.Max(Math.Max(mat[r1,c1], mat[r1,c2]),
                            Math.Max(mat[r2,c1], mat[r2,c2]));
        }
    }
    return sum;
}
 
// Driven Code
public static void Main()
{
    int[,] mat =
    {
        {112, 42, 83, 119},
        {56, 125, 56, 49},
        {15, 78, 101, 43},
        {62, 98, 114, 108}
    };
 
    Console.Write(maxSum(mat));
}
}
 
// This code is contributed
// by ChitraNayal


PHP


Javascript


输出:

414