📌  相关文章
📜  使所有相邻矩阵元素的总和均匀所需的最小增量

📅  最后修改于: 2021-09-06 06:39:06             🧑  作者: Mango

给定一个维度为N × M的矩阵mat[][] ,任务是最小化矩阵元素的增量数量,以使相邻矩阵元素的总和为偶数。
注意:对于任何矩阵元素mat[i][j],考虑mat[i – 1][j]、mat[i+1][j]、mat[i][j – 1]mat[i][ j + 1]作为其相邻元素。

例子:

办法:解决给定的问题的想法是基于这样的事实,两个元素的总和,即使只有当两个数字是偶数奇数。因此,对于相邻元素之和为偶数的矩阵,所有矩阵元素应具有相同的奇偶性,即全部为奇数或全部为偶数

因此,所需的最小增量数是给定矩阵中偶数奇数元素计数中的最小值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
const int MAX = 500;
 
// Function to find the minimum number
// of increments required to make sum of
// al adjacent matrix elements even
int minOperations(int mat[][MAX], int N)
{
    // Stores the count of odd elements
    int oddCount = 0;
 
    // Stores the count of even elements
    int evenCount = 0;
 
    // Iterate the matrix
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
 
            // If element is odd
            if (mat[i][j] & 1) {
 
                // Increment odd count
                oddCount++;
            }
 
            // Otherwise
            else {
 
                // Increment even count
                evenCount++;
            }
        }
    }
 
    // Print the minimum of both counts
    cout << min(oddCount, evenCount);
}
 
// Driver Code
int main()
{
    int mat[][MAX]
        = { { 1, 5, 6 }, { 4, 7, 8 }, { 2, 2, 3 } };
    int N = sizeof(mat) / sizeof(mat[0]);
 
    minOperations(mat, N);
 
    return 0;
}


Java
// Java program for the above approach
class GFG
{
 
static int MAX = 500;
 
// Function to find the minimum number
// of increments required to make sum of
// al adjacent matrix elements even
static void minOperations(int mat[][], int N)
{
   
    // Stores the count of odd elements
    int oddCount = 0;
 
    // Stores the count of even elements
    int evenCount = 0;
 
    // Iterate the matrix
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
 
            // If element is odd
            if (mat[i][j] % 2== 1) {
 
                // Increment odd count
                oddCount++;
            }
 
            // Otherwise
            else {
 
                // Increment even count
                evenCount++;
            }
        }
    }
 
    // Print the minimum of both counts
    System.out.print(Math.min(oddCount, evenCount));
}
 
// Driver Code
public static void main(String[] args)
{
    int mat[][]
        = { { 1, 5, 6 }, { 4, 7, 8 }, { 2, 2, 3 } };
    int N = mat.length;
    minOperations(mat, N);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python 3 program for the above approach
MAX = 500
 
# Function to find the minimum number
# of increments required to make sum of
# al adjacent matrix elements even
def minOperations(mat, N):
   
    # Stores the count of odd elements
    oddCount = 0
 
    # Stores the count of even elements
    evenCount = 0
 
    # Iterate the matrix
    for i in range(N):
        for j in range(N):
           
            # If element is odd
            if (mat[i][j] & 1):
               
                # Increment odd count
                oddCount += 1
 
            # Otherwise
            else:
               
                # Increment even count
                evenCount += 1
 
    # Print the minimum of both counts
    print(min(oddCount, evenCount))
 
# Driver Code
if __name__ == '__main__':
    mat =  [[1, 5, 6], [4, 7, 8], [2, 2, 3]]
    N = len(mat)
 
    minOperations(mat, N)
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
class GFG
{
  const int MAX = 500;
 
  // Function to find the minimum number
  // of increments required to make sum of
  // al adjacent matrix elements even
  static void minOperations(int[, ] mat, int N)
  {
    // Stores the count of odd elements
    int oddCount = 0;
 
    // Stores the count of even elements
    int evenCount = 0;
 
    // Iterate the matrix
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
 
        // If element is odd
        if ((mat[i, j] & 1) != 0) {
 
          // Increment odd count
          oddCount++;
        }
 
        // Otherwise
        else {
 
          // Increment even count
          evenCount++;
        }
      }
    }
 
    // Print the minimum of both counts
    Console.Write(Math.Min(oddCount, evenCount));
  }
 
  // Driver Code
  public static void Main()
  {
    int[, ] mat
      = { { 1, 5, 6 }, { 4, 7, 8 }, { 2, 2, 3 } };
    int N = mat.GetLength(0);
    minOperations(mat, N);
  }
}
 
// This code is contributed by chitranayal.


Javascript


输出:
4

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

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