给定NXM的两个二进制矩阵A [] []和B [] [] 。在一个单一的操作中,一个可以选择的子矩阵(2行和2c列分钟),并改变它的角元件的奇偶即1可以被改变为0和0可被改变为1。任务是检查是否可以使用任何数量的运算将矩阵A转换为B。
例子:
Input: A[][] = {{0, 1, 0}, {0, 1, 0}, {1, 0, 0}},
B[][] = {{1, 0, 0}, {1, 0, 0}, {1, 0, 0}}
Output: Yes
Choose the sub-matrix whose top-left corner is (0, 0)
and bottom-right corner is (1, 1).
Changing the parity of the corner elements
of this sub-matrix will convert A[][] to B[][]
Input: A[][] = {{0, 1, 0, 1}, {1, 0, 1, 0}, {0, 1, 0, 1}},
B[][] = {{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}}
Output: No
方法:首先要注意的是,尽管进行了转换,但两个矩阵的总奇偶校验将保持不变。因此,检查a [i] [j]是否与b [i] [j]不同,然后更改左上角为(0,0) ,右下角为( i,j)等于1≤i,j 。在对每个不等于b [i] [j]的a [i] [j]执行奇偶校验更改后,检查两个矩阵是否相同。如果它们相同,我们可以将A更改为B。
下面是上述方法的实现:
C++
// C++ implementation of the
// above appoach
#include
using namespace std;
#define N 3
#define M 3
// Boolean function that returns
// true or false
bool check(int a[N][M], int b[N][M])
{
// Traverse for all elements
for (int i = 1; i < N; i++) {
for (int j = 1; j < M; j++) {
// If both are not equal
if (a[i][j] != b[i][j]) {
// Change the parity of
// all corner elements
a[i][j] ^= 1;
a[0][0] ^= 1;
a[0][j] ^= 1;
a[i][0] ^= 1;
}
}
}
// Check if A is equal to B
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
// Not equal
if (a[i][j] != b[i][j])
return false;
}
}
return true;
}
// Driver Code
int main()
{
// First binary matrix
int a[N][N] = { { 0, 1, 0 },
{ 0, 1, 0 },
{ 1, 0, 0 } };
// Second binary matrix
int b[N][N] = { { 1, 0, 0 },
{ 1, 0, 0 },
{ 1, 0, 0 } };
if (check(a, b))
cout << "Yes";
else
cout << "No";
}
Java
// Java implementation of the
// above appoach
class GFG
{
static final int N = 3,M =3;
// Boolean function that returns
// true or false
static boolean check(int a[][], int b[][])
{
// Traverse for all elements
for (int i = 1; i < N; i++)
{
for (int j = 1; j < M; j++)
{
// If both are not equal
if (a[i][j] != b[i][j])
{
// Change the parity of
// all corner elements
a[i][j] ^= 1;
a[0][0] ^= 1;
a[0][j] ^= 1;
a[i][0] ^= 1;
}
}
}
// Check if A is equal to B
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
// Not equal
if (a[i][j] != b[i][j])
return false;
}
}
return true;
}
// Driver Code
public static void main(String args[])
{
// First binary matrix
int a[][] = { { 0, 1, 0 },
{ 0, 1, 0 },
{ 1, 0, 0 } };
// Second binary matrix
int b[][] = { { 1, 0, 0 },
{ 1, 0, 0 },
{ 1, 0, 0 } };
if (check(a, b))
System.out.print( "Yes");
else
System.out.print( "No");
}
}
// This code is contributed by Arnab Kundu
Python3
# Python 3 implementation of the
# above appoach
N = 3
M = 3
# Boolean function that returns
# true or false
def check(a, b):
# Traverse for all elements
for i in range(1, N, 1):
for j in range(1, M, 1):
# If both are not equal
if (a[i][j] != b[i][j]):
# Change the parity of
# all corner elements
a[i][j] ^= 1
a[0][0] ^= 1
a[0][j] ^= 1
a[i][0] ^= 1
# Check if A is equal to B
for i in range(N):
for j in range(M):
# Not equal
if (a[i][j] != b[i][j]):
return False
return True
# Driver Code
if __name__ == '__main__':
# First binary matrix
a = [[0, 1, 0],
[0, 1, 0],
[1, 0, 0]]
# Second binary matrix
b = [[1, 0, 0],
[1, 0, 0],
[1, 0, 0]]
if (check(a, b)):
print("Yes")
else:
print("No")
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of the
// above appoach
using System;
class GFG
{
static readonly int N = 3,M =3;
// Boolean function that returns
// true or false
static bool check(int [,]a, int [,]b)
{
// Traverse for all elements
for (int i = 1; i < N; i++)
{
for (int j = 1; j < M; j++)
{
// If both are not equal
if (a[i,j] != b[i,j])
{
// Change the parity of
// all corner elements
a[i,j] ^= 1;
a[0,0] ^= 1;
a[0,j] ^= 1;
a[i,0] ^= 1;
}
}
}
// Check if A is equal to B
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
// Not equal
if (a[i,j] != b[i,j])
return false;
}
}
return true;
}
// Driver Code
public static void Main(String []args)
{
// First binary matrix
int [,]a = { { 0, 1, 0 },
{ 0, 1, 0 },
{ 1, 0, 0 } };
// Second binary matrix
int [,]b = { { 1, 0, 0 },
{ 1, 0, 0 },
{ 1, 0, 0 } };
if (check(a, b))
Console.Write( "Yes");
else
Console.Write( "No");
}
}
// This code has been contributed by 29AjayKumar
PHP
Yes