📌  相关文章
📜  使所有相邻矩阵元素对不同所需的最小减量

📅  最后修改于: 2021-04-17 15:03:35             🧑  作者: Mango

给定尺寸为N * M的矩阵mat [] [] ,任务是计算所需的不同数组元素的最小递减数量,以使两个相邻的矩阵元素不相等。

例子:

方法:主要思想是通过假设矩阵如下图所示的国际象棋网格来解决给定的问题:

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

  1. 遍历矩阵
  2. 对于每个矩阵元素,都会出现以下两种情况:
    • 情况1:如果(i + j)是偶数,则mat [i] [j]应该是偶数。否则, mat [i] [j]应该是奇数。
    • 情况2:如果(i + j)为偶数,则mat [i] [j]上的值应为偶数。否则, mat [i] [j]的值应为奇数。
  3. 因此,请计算两种情况下所需的操作次数。
  4. 打印获得的两个计数中的最小值。

下面是上述方法的实现:

C++
// C++ program for
// the above approach
#include 
using namespace std;
 
// Matrix dimensions
const int n = 3;
const int m = 3;
 
// Function to count minimum
// number of operations required
void countDecrements(long long arr[][m])
{
    int count_1 = 0;
    int count_2 = 0;
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
 
            // Case 1:
            if ((i + j) % 2 == arr[i][j] % 2)
                count_1++;
 
            // Case 2:
            if (1 - (i + j) % 2 == arr[i][j] % 2)
                count_2++;
        }
    }
 
    // Print the minimum number
    // of operations required
    cout << min(count_1, count_2);
}
 
// Driver Code
int main()
{
    // The given matrix
    long long arr[][m]
        = { { 1, 2, 3 },
            { 1, 2, 3 },
            { 1, 2, 3 } };
 
    // Function Call to count
    // the minimum number of
    // decrements required
    countDecrements(arr);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to count minimum
  // number of operations required
  static void countDecrements(long arr[][])
  {
 
    // Matrix dimensions
    int n = arr.length;
    int m = arr[0].length;
 
    int count_1 = 0;
    int count_2 = 0;
 
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
 
        // Case 1:
        if ((i + j) % 2 == arr[i][j] % 2)
          count_1++;
 
        // Case 2:
        if (1 - (i + j) % 2 == arr[i][j] % 2)
          count_2++;
      }
    }
 
    // Print the minimum number
    // of operations required
    System.out.println(Math.min(count_1, count_2));
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    // The given matrix
    long arr[][] = { { 1, 2, 3 },
                    { 1, 2, 3 },
                    { 1, 2, 3 } };
 
    // Function Call to count
    // the minimum number of
    // decrements required
    countDecrements(arr);
  }
}
 
// This code is contributed by Kingash.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to count minimum
// number of operations required
static void countDecrements(long[,] arr)
{
     
    // Matrix dimensions
    int n = arr.GetLength(0);
    int m = arr.GetLength(1);
 
    int count_1 = 0;
    int count_2 = 0;
 
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
             
            // Case 1:
            if ((i + j) % 2 == arr[i, j] % 2)
                count_1++;
 
            // Case 2:
            if (1 - (i + j) % 2 == arr[i, j] % 2)
                count_2++;
        }
    }
 
    // Print the minimum number
    // of operations required
    Console.WriteLine(Math.Min(count_1, count_2));
}
 
// Driver Code
public static void Main()
{
     
    // The given matrix
    long[,] arr = { { 1, 2, 3 },
                    { 1, 2, 3 },
                    { 1, 2, 3 } };
 
    // Function Call to count
    // the minimum number of
    // decrements required
    countDecrements(arr);
}
}
 
// This code is contributed by ukasp


Javascript


输出:
3

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