给定尺寸为N * M的二进制矩阵mat [] [] ,任务是检查每行中的所有1是否都在给定矩阵上相邻放置。如果每行中的所有1都相邻,则打印“是” 。否则,打印“否” 。
例子:
Input: mat[][] = {{0, 1, 1, 0}, {1, 1, 0, 0}, {0, 0, 0, 1}, {1, 1, 1, 0}
Output: Yes
Explanation:
Elements in the first row are {0, 1, 1, 0}.
Elements in the 2nd row are {1, 1, 0, 0}.
Elements in the 3rd row are {0, 0, 0, 1}.
Elements in the 4th row are {1, 1, 1, 0}.
Therefore, all the rows have all 1s grouped together. Therefore, print Yes.
Input: mat[][] = {{1, 0, 1}, {0, 0, 1}, {0, 0, 0}}
Output: No
方法:想法是在矩阵上执行逐行遍历,并通过使用按位XOR属性检查一行中的所有1 s是否相邻放置。可以根据以下观察结果解决给定的问题:
- 计算每对第i行的相邻元件中的位异或的总和,说X。如果满足以下任一条件,则第i行中的所有1都不在一起:
- 如果X> 2并且mat [i] [0] + mat [i] [M – 1] = 0 。
- 如果X> 1并且mat [i] [0] + mat [i] [M – 1] = 1 。
- 如果X> 0并且mat [i] [0] + mat [i] [M – 1] = 0 。
请按照以下步骤解决此问题:
- 遍历给定的矩阵mat [] []并执行以下操作:
- 对于每一行,检查M的值是否小于3 ,然后打印“是” 。
- 否则,找到相邻数组元素的按位XOR之和并将其存储在变量中,例如X。
- 对于X的每个值,如果任何上述条件成立,那么打印“否”。
- 完成上述步骤后,如果以上条件中的任何一个对于X的任何值都不成立,则打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if all 1s are
// placed adjacently in an array or not
bool checkGroup(vector arr)
{
// Base Case
if (arr.size() <= 2)
return true;
int corner = arr[0] + arr[(int)arr.size()-1];
// Stores the sum of XOR of all
// pair of adjacent elements
int xorSum = 0;
// Calculate sum of XOR of all
// pair of adjacent elements
for (int i = 0; i < arr.size() - 1; i++)
xorSum += (arr[i] ^ arr[i + 1]);
// Check for corner cases
if (!corner)
if (xorSum > 2)
return false;
else if (corner == 1)
if (xorSum > 1)
return false;
else
if (xorSum > 0)
return false;
// Return true
return true;
}
// Function to check if all the rows
// have all 1s grouped together or not
bool isInGroupUtil(vector> mat)
{
// Traverse each row
for (auto i:mat)
{
// Check if all 1s are placed
// together in the ith row or not
if (!checkGroup(i))
return false;
}
return true;
}
// Function to check if all 1s in a row
// are grouped together in a matrix or not
void isInGroup(vector> mat)
{
bool ans = isInGroupUtil(mat);
//Print the result
if (ans)
printf("Yes");
else
printf("No");
}
// Driver Code
int main()
{
// Given matrix
vector> mat = {{0, 1, 1, 0},
{1, 1, 0, 0},
{0, 0, 0, 1},
{1, 1, 1, 0}};
// Function Call
isInGroup(mat);
}
// This code is contributed by mohit kumar 29.
Python3
# Python3 program for the above approach
# Function to check if all 1s are
# placed adjacently in an array or not
def checkGroup(arr):
# Base Case
if len(arr) <= 2:
return True
corner = arr[0] + arr[-1]
# Stores the sum of XOR of all
# pair of adjacent elements
xorSum = 0
# Calculate sum of XOR of all
# pair of adjacent elements
for i in range(len(arr)-1):
xorSum += (arr[i] ^ arr[i + 1])
# Check for corner cases
if not corner:
if xorSum > 2:
return False
elif corner == 1:
if xorSum > 1:
return False
else:
if xorSum > 0:
return False
# Return true
return True
# Function to check if all the rows
# have all 1s grouped together or not
def isInGroupUtil(mat):
# Traverse each row
for i in mat:
# Check if all 1s are placed
# together in the ith row or not
if not checkGroup(i):
return False
return True
# Function to check if all 1s in a row
# are grouped together in a matrix or not
def isInGroup(mat):
ans = isInGroupUtil(mat)
# Print the result
if ans:
print("Yes")
else:
print("No")
# Given matrix
mat = [[0, 1, 1, 0], [1, 1, 0, 0],
[0, 0, 0, 1], [1, 1, 1, 0]]
# Function Call
isInGroup(mat)
输出:
Yes
时间复杂度: O(N * M)
辅助空间: O(1)