通过翻转大小为 K 的子矩阵将二进制矩阵 A 转换为 B 的最小操作
给定两个维度为N * M的二进制矩阵A[]和B[]以及一个正整数K ,任务是找到矩阵A[][]中所需的大小为K的子矩阵的最小翻转次数,以将其转换为矩阵B[][] 。如果无法转换,则打印“-1” 。
例子:
Input: A[][] = { { ‘1’, ‘1’, ‘1’ }, { ‘1’, ‘1’, ‘1’ }, { ‘1’, ‘1’, ‘1’ } }, B[][] = { { ‘0’, ‘0’, ‘0’ }, { ‘0’, ‘0’, ‘0’ }, { ‘0’, ‘0’, ‘0’ } }, K = 3
Output: 1
Explanation:
Following are the operations performed:
Operation 1: Flip the submatrix of size K from indices (0, 0) modifies the matrix A[][] to { { ‘0’, ‘0’, ‘0’ }, { ‘0’, ‘0’, ‘0’ }, { ‘0’, ‘0’, ‘0’ } }.
Therefore, the minimum number of flips required is 1.
Input: A[][] = { { ‘1’, ‘0’, ‘0’ }, { ‘0’, ‘0’, ‘0’ }, { ‘0’, ‘0’, ‘0’ } }, B[][] = { { ‘0’, ‘0’, ‘0’ }, { ‘0’, ‘0’, ‘0’ }, { ‘0’, ‘0’, ‘0’ } }, K = 3
Output: -1
方法:给定的问题可以使用贪心方法来解决,想法是遍历给定的矩阵A[][]和B[][] ,如果相应的单元格(i, j)具有不同的值,则翻转当前来自索引(i, j)的大小为K的子矩阵并计算此翻转操作。遍历给定矩阵后,如果存在任何索引使得大小为K的子矩阵无法翻转,则打印“-1” ,因为无法将矩阵A[][]转换为B[][] 。否则,打印获得的操作计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the operations
// required to convert matrix A to B
int minMoves(vector> a,
vector> b,
int K)
{
// Store the sizes of matrix
int n = a.size(), m = a[0].size();
// Stores count of flips required
int cntOperations = 0;
// Traverse the given matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// Check if the matrix values
// are not equal
if (a[i][j] != b[i][j]) {
// Increment the count
// of moves
cntOperations++;
// Check if the current
// square sized exists
// or not
if (i + K - 1 >= n
|| j + K - 1 >= m) {
return -1;
}
// Flip all the bits in this
// square sized submatrix
for (int p = 0;
p <= K - 1; p++) {
for (int q = 0;
q <= K - 1; q++) {
if (a[i + p][j + q] == '0') {
a[i + p][j + q] = '1';
}
else {
a[i + p][j + q] = '0';
}
}
}
}
}
}
// Count of operations required
return cntOperations;
}
// Driver Code
int main()
{
vector > A = { { '1', '0', '0' },
{ '0', '0', '0' },
{ '0', '0', '0' } };
vector > B = { { '0', '0', '0' },
{ '0', '0', '0' },
{ '0', '0', '0' } };
int K = 3;
cout << minMoves(A, B, K);
return 0;
}
Java
// Java program for the above approach
public class GFG {
// Function to count the operations
// required to convert matrix A to B
static int minMoves(char a[][],char b[][], int K)
{
// Store the sizes of matrix
int n = a.length;
int m = a[0].length;
// Stores count of flips required
int cntOperations = 0;
// Traverse the given matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// Check if the matrix values
// are not equal
if (a[i][j] != b[i][j]) {
// Increment the count
// of moves
cntOperations++;
// Check if the current
// square sized exists
// or not
if (i + K - 1 >= n
|| j + K - 1 >= m) {
return -1;
}
// Flip all the bits in this
// square sized submatrix
for (int p = 0;
p <= K - 1; p++) {
for (int q = 0;
q <= K - 1; q++) {
if (a[i + p][j + q] == '0') {
a[i + p][j + q] = '1';
}
else {
a[i + p][j + q] = '0';
}
}
}
}
}
}
// Count of operations required
return cntOperations;
}
// Driver Code
public static void main (String[] args)
{
char A[][] = { { '1', '0', '0' },
{ '0', '0', '0' },
{ '0', '0', '0' } };
char B[][] = { { '0', '0', '0' },
{ '0', '0', '0' },
{ '0', '0', '0' } };
int K = 3;
System.out.println(minMoves(A, B, K));
}
}
// This code is contributed by AnkThon
Python3
# python program for the above approach
# Function to count the operations
# required to convert matrix A to B
def minMoves(a, b, K):
# Store the sizes of matrix
n = len(a)
m = len(a[0])
# Stores count of flips required
cntOperations = 0
# Traverse the given matrix
for i in range(0, n):
for j in range(0, m):
# Check if the matrix values
# are not equal
if (a[i][j] != b[i][j]):
# Increment the count
# of moves
cntOperations += 1
# Check if the current
# square sized exists
# or not
if (i + K - 1 >= n or j + K - 1 >= m):
return -1
# Flip all the bits in this
# square sized submatrix
for p in range(0, K):
for q in range(0, K):
if (a[i + p][j + q] == '0'):
a[i + p][j + q] = '1'
else:
a[i + p][j + q] = '0'
# Count of operations required
return cntOperations
# Driver Code
if __name__ == "__main__":
A = [['1', '0', '0'], ['0', '0', '0'], ['0', '0', '0']]
B = [['0', '0', '0'], ['0', '0', '0'], ['0', '0', '0']]
K = 3
print(minMoves(A, B, K))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
class GFG
{
// Function to count the operations
// required to convert matrix A to B
static int minMoves(char[,] a, char[,] b, int K)
{
// Store the sizes of matrix
int n = a.Length;
int m = a.GetLength(0);
// Stores count of flips required
int cntOperations = 0;
// Traverse the given matrix
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
// Check if the matrix values
// are not equal
if (a[i, j] != b[i, j])
{
// Increment the count
// of moves
cntOperations++;
// Check if the current
// square sized exists
// or not
if (i + K - 1 >= n
|| j + K - 1 >= m)
{
return -1;
}
// Flip all the bits in this
// square sized submatrix
for (int p = 0;
p <= K - 1; p++)
{
for (int q = 0;
q <= K - 1; q++)
{
if (a[i + p, j + q] == '0')
{
a[i + p, j + q] = '1';
}
else
{
a[i + p, j + q] = '0';
}
}
}
}
}
}
// Count of operations required
return cntOperations;
}
// Driver Code
public static void Main()
{
char[,] A = new char[,]{ { '1', '0', '0' },
{ '0', '0', '0' },
{ '0', '0', '0' } };
char[,] B = new char[,]{ { '0', '0', '0' },
{ '0', '0', '0' },
{ '0', '0', '0' } };
int K = 3;
Console.WriteLine(minMoves(A, B, K));
}
}
// This code is contributed by Saurabh.
Javascript
-1
时间复杂度: O(N*M*K 2 )
辅助空间: O(1)