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

📅  最后修改于: 2021-10-26 06:58:13             🧑  作者: 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.


Python3
# Python program for
# the above approach
 
# Matrix dimensions
n = 3
m = 3
 
# Function to count minimum
# number of operations required
def countDecrements(arr):
    count_1 = 0
    count_2 = 0
 
    for i in range(n):
        for j in range(m):
 
            # Case 1:
            if ((i + j) % 2 == arr[i][j] % 2):
                count_1 += 1
 
            # Case 2:
            if (1 - (i + j) % 2 == arr[i][j] % 2):
                count_2 += 1
 
    # Print the minimum number
    # of operations required
    print(min(count_1, count_2))
 
# Driver Code
# The given matrix
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 souravmahato348.


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)