最难的才有挑战性,才值得学习!
小蓝有一个01矩阵。他打算将第一行第一列的 0 变为 2 。变化过程有传染性,每次 2 的上下左右四个相邻的位置中的 0 都会变成 2 。直到最后每个 2 的周围都是 1 或 2 结束。 请问,最终矩阵中有多少个 2 ? 以下是小蓝的矩阵,共 30 行 40 列。 0000100010000001101010101001001100000011 0101111001111101110111100000101010011111 1000010000011101010110000000001011010100 0110101010110000000101100100000101001001 0000011010100000111111001101100010101001 0110000110000000110100000000010010100011 0100110010000110000000100010000101110000 0010011010100110001111001101100110100010 1111000111101000001110010001001011101101 0011110100011000000001101001101110100001 0000000101011000010011111001010011011100 0000100000011001000100101000111011101100 0010110000001000001010100011000010100011 0110110000100011011010011010001101011011 0000100100000001010000101100000000000010 0011001000001000000010011001100101000110 1110101000011000000100011001001100111010 0000100100111000001101001000001010010001 0100010010000110100001100000110111110101 1000001001100010011001111101011001110001 0000000010100101000000111100110010101101 0010110101001100000100000010000010110011 0000011101001001000111011000100111010100 0010001100100000011000101011000000010101 1001111010010110011010101110000000101110 0110011101000010100001000101001001100010 1101000000010010011001000100110010000101 1001100010100010000100000101111111111100 1001011010101100001000000011000110110000 0011000100011000010111101000101110110001
def spread_twos(matrix):
rows = len(matrix)
cols = len(matrix[0])
def propagate(r, c):
if matrix[r][c] == 0:
matrix[r][c] = 2
if r > 0: propagate(r - 1, c)
if r < rows - 1: propagate(r + 1, c)
if c > 0: propagate(r, c - 1)
if c < cols - 1: propagate(r, c + 1)
propagate(0, 0)
return sum(row.count(2) for row in matrix)
matrix = [
[int(char) for char in line]
for line in [
"0000100010000001101010101001001100000011",
"0101111001111101110111100000101010011111",
"1000010000011101010110000000001011010100",
"0110101010110000000101100100000101001001",
"0000011010100000111111001101100010101001",
"0110000110000000110100000000010010100011",
"0100110010000110000000100010000101110000",
"0010011010100110001111001101100110100010",
"1111000111101000001110010001001011101101",
"0011110100011000000001101001101110100001",
"0000000101011000010011111001010011011100",
"0000100000011001000100101000111011101100",
"0010110000001000001010100011000010100011",
"0110110000100011011010011010001101011011",
"0000100100000001010000101100000000000010",
"0011001000001000000010011001100101000110",
"1110101000011000000100011001001100111010",
"0000100100111000001101001000001010010001",
"0100010010000110100001100000110111110101",
"1000001001100010011001111101011001110001",
"0000000010100101000000111100110010101101",
"0010110101001100000100000010000010110011",
"0000011101001001000111011000100111010100",
"0010001100100000011000101011000000010101",
"1001111010010110011010101110000000101110",
"0110011101000010100001000101001001100010",
"1101000000010010011001000100110010000101",
"1001100010100010000100000101111111111100",
"1001011010101100001000000011000110110000",
"0011000100011000010111101000101110110001"
]
]
number_of_twos = spread_twos(matrix)
print(f"最终矩阵中有 {number_of_twos} 个2。")
-
深度优先搜索 (
dfs
)函数- 从指定的位置开始,标记当前单元格为2,并递归搜索相邻的单元格,将连接的零区域也标记为2。
- 递归的停止条件是当越界或者当前单元格不为0时停止。
-
计算连通区域数量 (
count_twos
)函数- 从第一行和第一列开始,对值为0的单元格调用
dfs
函数,标记与其连通的零区域为2。 - 然后统计整个矩阵中值为2的单元格的数量,即为连通的零区域数量。
- 从第一行和第一列开始,对值为0的单元格调用
-
数据处理
- 提供了一个包含大量 0 和 1 的字符串数据,代表了初始矩阵。
- 将这些数据转换为二维列表,然后传递给
count_twos
函数。
总体来说,这段代码通过深度优先搜索算法在二维矩阵中查找连通的零区域,并统计这些区域的数量。
难度系数:5星。文章来源:https://www.toymoban.com/news/detail-756241.html
答案:541。文章来源地址https://www.toymoban.com/news/detail-756241.html
到了这里,关于第十五届蓝桥杯模拟赛(第二期)第5题(Python)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!