Python 检查二进制矩阵的水平和垂直对称性
二进制矩阵是一个矩形网格,每个元素可以是0或1,表示真或假状态。它被广泛应用于表示各个学科领域的关系、连通性和模式。
假设我们有一个包含N行和M列的2D二进制输入矩阵。现在,我们将使用以下方法检查输入矩阵是否具有水平或垂直对称性,或者两者都有。
如果第一行与最后一行相匹配,第二行与倒数第二行相匹配,依此类推,矩阵被称为水平对称。
如果第一列与最后一列相匹配,第二列与倒数第二列相匹配,依此类推,矩阵被称为垂直对称。
示例
1 0 1
0 1 0
1 0 1
水平对称性:第一行“1 0 1”在矩阵水平翻转时,与第三行“1 0 1”是一模一样的镜像反射。第一行的每个元素与第三行相同位置的元素对应。
垂直对称性:第一列“1 0 1”在矩阵垂直翻转时,与第三列“1 0 1”是一模一样的镜像版本。第一列的元素与第三列相同位置的元素对齐。
步骤
下面是执行所需任务的算法/步骤:
- 创建一个函数 checkHorizontalVertical() 通过传入输入矩阵,行数和列数作为参数来检查输入矩阵是否在水平或垂直方向上对称。
-
将horizontal_symmetric和vertical_symmetric初始化为True,假设矩阵在两个方向上都对称。
-
将第一行与最后一行进行比较,将第二行与倒数第二行进行比较,依此类推,以检查水平对称性。
-
遍历矩阵的前半部分行。
-
在循环中,遍历列,并将当前行的每个单元格与正在比较的行中对应的单元格进行比较。
-
如果任何单元格不同,将horizontal_symmetric设置为False并终止循环。
-
将行号加1,将行号从最后一行减1以进行下一次迭代。
-
接下来,通过将第一列与最后一列进行比较,将第二列与倒数第二列进行比较,依此类推来检查垂直对称性。
-
遍历矩阵的前半部分列。
-
在循环中,遍历行,并将当前列的每个单元格与正在比较的列中对应的单元格进行比较。
-
如果任何单元格不同,将vertical_symmetric设置为False并终止循环。
-
将列号加1,将列号从最后一列减1以进行下一次迭代。
-
检查对称性的条件:
- 如果horizontal_symmetric和vertical_symmetric都不是True,打印“既不水平也不垂直对称”。
-
如果horizontal_symmetric为True但vertical_symmetric为False,打印“输入矩阵是水平对称的”。
-
如果vertical_symmetric为True但horizontal_symmetric为False,打印“输入矩阵是垂直对称的”。
-
如果horizontal_symmetric和vertical_symmetric都为True,打印“输入矩阵既水平又垂直对称”。
-
使用inputMatrix、行数和列数作为参数调用checkHorizontalVertical函数。
示例
下面给出的示例将行与其末尾相对应的行进行比较,以检查是否具有水平对称性,并将列与其末尾相对应的列进行比较,以检查是否具有垂直对称性。根据比较结果,它打印出输入矩阵是水平对称、垂直对称、水平和垂直同时对称,还是既不水平对称也不垂直对称。
# Creating a function to check whether the input matrix is
# horizontal or vertically symmetric by passing the input matrix,
# no of rows and columns as arguments
def checkHorizontalVertical(inputMatrix, rows, cols):
# Initializing both the horizontal and vertical
# symmetric as true at first
horizontal_symmetric = True
vertical_symmetric = True
# We compare the first row with the last row, the second row with second
# last row and so on.
p = 0
x = rows - 1
# Traversing till half of the rows of the matrix
while p < rows // 2:
# Traversing in the columns
for q in range(cols):
# Checking if each cell is the same or not
if inputMatrix[p][q] != inputMatrix[x][q]:
# If it is not the same then that horizontal symmetric is false
horizontal_symmetric = False
break
# Incrementing the row number by 1
p += 1
x -= 1
# Checking for Vertical Symmetry. The first column is compared to the last column,
# the second column to the second-to-last column, and so forth.
p = 0
x = cols - 1
# Traversing till half of the columns
while p < cols // 2:
# Checking each row of the column
for q in range(rows):
# Checking if each cell is the same or not
if inputMatrix[p][q] != inputMatrix[x][q]:
# If it is not the same then that vertical symmetric is false
vertical = False
break
# Incrementing the column number by 1
p += 1
x -= 1
# checking whether not horizontal and not vertically symmetric
if not horizontal_symmetric and not vertical_symmetric:
# printing Neither horizontal nor vertical symmetric if the condition is true
print("Neither horizontal nor vertical symmetric")
# checking whether the matrix is horizontally symmetric but not vertical
elif horizontal_symmetric and not vertical_symmetric:
# printing horizontal symmetric matrix
print("The input matrix is horizontal symmetric")
# checking whether the matrix is vertically symmetric but not horizontal
elif vertical_symmetric and not horizontal_symmetric:
# printing vertically symmetric
print("The input matrix is vertical symmetric")
else:
# else printing both horizontal and vertical symmetric
print("The input matrix is both horizontal and vertically symmetric")
# input matrix
inputMatrix = [[1, 0, 1], [0, 0, 0], [1, 0, 1]]
# calling the above checkHorizontalVertical() by passing
# input matrix, no of rows, columns as arguments
checkHorizontalVertical(inputMatrix, 3, 3)
输出
The input matrix is both horizontal and vertically symmetric
结论
在这篇文章中,我们讲解了如何使用嵌套的for循环遍历矩阵,以及如何使用for循环遍历每一列的行。最后,我们学习了如何判断一个矩阵是否在水平方向或垂直方向上对称。