将给定的二进制矩阵 A 转换为二进制矩阵 B 所需的最小交换
给定两个二进制矩阵, A[][]和B[][] 大小为N×M,任务是找到将矩阵A转换为矩阵B所需的矩阵 A 的元素的最小交换次数。如果不可能这样做,则打印“ -1 ”。
例子 :
Input: A[][] = {{1, 1, 0}, {0, 0, 1}, {0, 1, 0}}, B[][] = {{0, 0, 1}, {0, 1, 0}, {1, 1, 0}}
Output: 3
Explanation:
One possible way to convert matrix A into B is:
- Swap the element A[0][0] with A[0][2]. Thereafter the matrix A modifies to {{ 0, 1, 1}, {0, 0, 1}, {0, 1, 0}}.
- Swap the element A[0][1] with A[1][1]. Thereafter the matrix A modifies to {{ 0, 0, 1}, {0, 1, 1}, {0, 1, 0}}.
- Swap the element A[1][2] with A[2][0]. Thereafter the matrix A modifies to {{ 0, 0, 1}, {0, 1, 0}, {1, 1, 0}}.
Therefore, the total number of moves needed is 3 and also it is the minimum number of moves needed.
Input: A[][] = {{1, 1}, {0, 1}, {1, 0}, {0, 0}}, B[][] = {{1, 1}, {1, 1}, {0, 1}, {0, 0}}
Output: -1
朴素的方法:这个问题可以使用散列来解决。要仅通过交换将矩阵 A 转换为 B,两个矩阵中的设置位和未设置位的计数必须相同。所以首先检查 A 中的设置位和未设置位是否与 B 中的相同。如果是,则找出 A 中的元素与 B 中的元素不同的位置数。这将是最终计数。
有效方法:上述方法可以进一步优化空间,借助观察计算A[i][j] = 0和B[i][j] = 1的元素数量和元素数量,使得A[i][j] = 1和B[i][j] = 0必须相等,并且所需的最小移动次数等于获得的计数。请按照以下步骤解决问题:
- 初始化两个变量,比如count10和count01 ,它们计算元素的数量,使得A[i][j] = 1和B[i][j] = 0以及元素的数量,使得A{i][j] = 0和B[i][j] = 1分别。
- 使用变量i遍历范围[0, N-1]并执行以下步骤:
- 使用变量j迭代范围[0, M-1]并且如果A[i][j] = 1且B[i][j] = 0然后将count10增加1。否则,如果A[i] [j] = 0和B[i][j] = 1然后将count01增加1 。
- 如果count01等于count10 ,则打印count01的值作为答案。否则,打印-1作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the minimum number
// of swaps required to convert matrix
// A to matrix B
int minSwaps(int N, int M, vector >& A,
vector >& B)
{
// Stores number of cells such that
// matrix A contains 0 and matrix B
// contains 1
int count01 = 0;
// Stores number of cells such that
// matrix A contains 1 and matrix B
// contains 0
int count10 = 0;
// Iterate over the range [0, N-1]
for (int i = 0; i < N; i++) {
// Iterate over the range [0, M-1]
for (int j = 0; j < M; j++) {
if (A[i][j] != B[i][j]) {
// If A[i][j] = 1 and B[i][j] = 0
if (A[i][j] == 1)
count10++;
// If A[i][j] = 0 and B[i][j] = 1
else
count01++;
}
}
}
// If count01 is equal to count10
if (count01 == count10)
return count01;
// Otherwise,
else
return -1;
}
// Driver Code
int main()
{
vector > A
= { { 1, 1, 0 }, { 0, 0, 1 }, { 0, 1, 0 } };
vector > B
= { { 0, 0, 1 }, { 0, 1, 0 }, { 1, 1, 0 } };
int N = A.size();
int M = B[0].size();
cout << minSwaps(N, M, A, B);
}
Java
// Java program for the above approach
public class MyClass
{
// Function to count the minimum number
// of swaps required to convert matrix
// A to matrix B
public static int minSwaps(int N, int M, int A[][], int B[][])
{
// Stores number of cells such that
// matrix A contains 0 and matrix B
// contains 1
int count01 = 0;
// Stores number of cells such that
// matrix A contains 1 and matrix B
// contains 0
int count10 = 0;
// Iterate over the range [0, N-1]
for (int i = 0; i < N; i++) {
// Iterate over the range [0, M-1]
for (int j = 0; j < M; j++) {
if (A[i][j] != B[i][j]) {
// If A[i][j] = 1 and B[i][j] = 0
if (A[i][j] == 1)
count10++;
// If A[i][j] = 0 and B[i][j] = 1
else
count01++;
}
}
}
// If count01 is equal to count10
if (count01 == count10)
return count01;
// Otherwise,
else
return -1;
}
// Driver Code
public static void main(String args[])
{
int [][] A = { { 1, 1, 0 }, { 0, 0, 1 }, { 0, 1, 0 } };
int [][] B = { { 0, 0, 1 }, { 0, 1, 0 }, { 1, 1, 0 } };
int N = A.length;
int M = B[0].length;
System.out.println(minSwaps(N, M, A, B));
}}
// This code is contributed by SoumikMondal
Python3
# Python3 program for the above approach
# Function to count the minimum number
# of swaps required to convert matrix
# A to matrix B
def minSwaps(N, M, A, B):
# Stores number of cells such that
# matrix A contains 0 and matrix B
# contains 1
count01 = 0
# Stores number of cells such that
# matrix A contains 1 and matrix B
# contains 0
count10 = 0
# Iterate over the range[0, N-1]
for i in range(0, N):
# Iterate over the range[0, M-1]
for j in range(0, M):
if (A[i][j] != B[i][j]):
# If A[i][j] = 1 and B[i][j] = 0
if (A[i][j] == 1):
count10 += 1
# If A[i][j] = 0 and B[i][j] = 1
else:
count01 += 1
# If count01 is equal to count10
if (count01 == count10):
return count01
# Otherwise,
else:
return -1
# Driver Code
A = [ [ 1, 1, 0 ], [ 0, 0, 1 ], [ 0, 1, 0 ] ]
B = [ [ 0, 0, 1 ], [ 0, 1, 0 ], [ 1, 1, 0 ] ]
N = len(A)
M = len(B[0])
print(minSwaps(N, M, A, B))
# This code is contributed by amreshkumar3
Javascript
C#
// C# program for the above approach
using System;
class GFG {
// Function to count the minimum number
// of swaps required to convert matrix
// A to matrix B
public static int minSwaps(int N, int M, int[,] A, int[,] B)
{
// Stores number of cells such that
// matrix A contains 0 and matrix B
// contains 1
int count01 = 0;
// Stores number of cells such that
// matrix A contains 1 and matrix B
// contains 0
int count10 = 0;
// Iterate over the range [0, N-1]
for (int i = 0; i < N; i++) {
// Iterate over the range [0, M-1]
for (int j = 0; j < M; j++) {
if (A[i,j] != B[i, j]) {
// If A[i][j] = 1 and B[i][j] = 0
if (A[i, j] == 1)
count10++;
// If A[i][j] = 0 and B[i][j] = 1
else
count01++;
}
}
}
// If count01 is equal to count10
if (count01 == count10)
return count01;
// Otherwise,
else
return -1;
}
// Driver code
public static void Main (String[] args)
{
int [,] A = { { 1, 1, 0 }, { 0, 0, 1 }, { 0, 1, 0 } };
int [,] B = { { 0, 0, 1 }, { 0, 1, 0 }, { 1, 1, 0 } };
int N = A.GetLength(0);
int M = 3;
Console.Write(minSwaps(N, M, A, B));
}
}
3
时间复杂度: O(N*M)。
辅助空间: O(1)