gpt4 book ai didi

python - 需要帮助修改洪水填充算法

转载 作者:太空宇宙 更新时间:2023-11-03 17:00:53 25 4
gpt4 key购买 nike

我正在尝试修改洪水填充算法以返回最终的二维矩阵,其中所有 color1 仅用 color2 着色。泛洪应该从矩阵中的 x, y 开始。

测试用例1:

之前:

matrix = [[4, 3, 1, 2],
[3, 1, 1, 2],
[1, 2, 4, 5]]

matrix = fill(matrix, x = 0, y = 2, color1 = 1, color2 = 2)之后

matrix = [[4, 3, 2, 2],
[3, 2, 2, 2],
[1, 2, 4, 5]]

测试用例2:

之前:

matrix = [[3, 2, 4],
[5, 1, 4],
[4, 3, 1]]

matrix = fill(matrix, x = 0, y = 0, color1 = 3, color2 = 1)之后

matrix = [[1, 2, 4],
[5, 1, 4],
[4, 3, 1]]

测试用例3:

之前:

matrix = [[2, 1, 1],
[2, 1, 2],
[2, 2, 2]]

matrix = fill(matrix, x = 1, y = 2, color1 = 2, color2 = 1)之后

matrix = [[1, 1, 1],
[1, 1, 1],
[1, 1, 1]]

这与我在Invent with Python Blog发现的僵尸感染问题非常相似。

目前,我有只修改全局矩阵的算法。

def fill(matrix, x, y, color1, color2):

matWidth = len(matrix)
matHeight = len(matrix[0])
if x < 0 or y < 0 or x >= matWidth or y >= matHeight:
return

if matrix[x][y] == color2 or matrix[x][y] != color1:
return
if matrix[x][y] == color1:
matrix[x][y] = color2

fill(matrix, x - 1, y, color1, color2)
fill(matrix, x + 1, y, color1, color2)
fill(matrix, x, y - 1, color1, color2)
fill(matrix, x, y + 1, color1, color2)

有没有办法修改 fill() ,使其以矩阵作为参数并返回最终填充的矩阵?

非常感谢!

我已经非常接近解决这个问题了。这是我的解决方案:

def fill(matrix, x, y, color1, color2):
matWidth = len(matrix)
matHeight = len(matrix[0])

if x < 0 or y < 0 or x >= matWidth or y >= matHeight:
return matrix

if mat[x][y] != color1:
return matrix

else:
matrix[x][y] = color2
if x == 0:
if y == 0:
if matrix[x + 1][y] == color1 and color[x + 1][y] != color2:
matrix = fill(matrix, x + 1, y, color1, color2)
if matrix[x][y + 1] == color1 and matrix[x][y + 1] != color2:
matrix = fill(matrix, x, y + 1, color1, color2)
if y == matHeight - 1:
if matrix[x][y - 1] == color1 and matrix[x][y - 1] != color2:
matrix = fill(matrix, x, y - 1, color1, color2)
if matrix[x + 1][y] == color1 and matrix[x + 1][y] != color2:
matrix = fill(matrix, x + 1, y, color1, color2)
else:
if matrix[x][y - 1] == color1 and matrix[x][y - 1] != color2:
matrix = fill(matrix, x, y - 1, color1, color2)
if matrix[x][y + 1] == color1 and matrix[x][y + 1] != color2:
matrix = fill(matrix, x, y + 1, color1, color2)
if matrix[x + 1][y] == color1 and matrix[x + 1][y] != color2:
matrix = fill(matrix, x + 1, y, color1, color2)
if x == matWidth - 1:
if y == 0:
if matrix[x - 1][y] == color1 and matrix[x - 1][y] != color2:
matrix = fill(matrix, x - 1, y, color1, color2)
if matrix[x][y + 1] == color1 and matrix[x][y + 1] != color2:
matrix = fill(matrix, x, y + 1, color1, color2)
if y == matHeight - 1:
if matrix[x][y - 1] == color1 and matrix[x][y - 1] != color2:
matrix = fill(matrix, x, y - 1, color1, color2)
if matrix[x - 1][y] == color1 and matrix[x - 1][y] != color2:
matrix = fill(matrix, x - 1, y, color1, color2)
else:
if matrix[x][y - 1] == color1 and matrix[x][y - 1] != color2:
matrix = fill(matrix, x, y - 1, color1, color2)
if matrix[x][y + 1] == color1 and matrix[x][y + 1] != color2:
matrix = fill(matrix, x, y + 1, color1, color2)
if matrix[x - 1][y] == color1 and matrix[x - 1][y] != color2:
matrix = fill(matrix, x - 1, y, color1, color2)

if y > 0 and matrix[x][y-1] == color1 and matrix[x][y-1] != color2:
matrix = fill(matrix, x, y-1, color1, color2)

if y < matHeight and matrix[x][y+1] == color1 and matrix[x][y+1] != color2:
matrix = fill(matrix, x, y-1, color1, color2)

if x < matWidth and matrix[x+1][y] == color1 and matrix[x+1][y] != color2:
matrix = fill(matrix, x+1, y, color1, color2)

if x > 0 and matrix[x-1][y] == color1 and matrix[x-1][y] != color2:
matrix = fill(matrix, x-1, y, color1, color2)

return matrix

如有任何帮助,我们将不胜感激。谢谢

最佳答案

简单地返回矩阵:

def fill(matrix, x, y, color1, color2):

matWidth = len(matrix)
matHeight = len(matrix[0])
if x < 0 or y < 0 or x >= matWidth or y >= matHeight:
return matrix

if matrix[x][y] == color2 or matrix[x][y] != color1:
return matrix
if matrix[x][y] == color1:
matrix[x][y] = color2

fill(matrix, x - 1, y, color1, color2)
fill(matrix, x + 1, y, color1, color2)
fill(matrix, x, y - 1, color1, color2)
fill(matrix, x, y + 1, color1, color2)

return matrix

或者,如果您不喜欢总是返回它,请使用包装器仅在最后返回一次:<​​/p>

def fill(matrix, x, y, color1, color2):
def fill(matrix, x, y, color1, color2):

matWidth = len(matrix)
matHeight = len(matrix[0])
if x < 0 or y < 0 or x >= matWidth or y >= matHeight:
return

if matrix[x][y] == color2 or matrix[x][y] != color1:
return
if matrix[x][y] == color1:
matrix[x][y] = color2

fill(matrix, x - 1, y, color1, color2)
fill(matrix, x + 1, y, color1, color2)
fill(matrix, x, y - 1, color1, color2)
fill(matrix, x, y + 1, color1, color2)
fill(matrix, x, y, color1, color2)
return matrix

在这种情况下,您还可以删除大多数参数。这是我这样做的一个版本,并且还使代码变得更简单:

def fill(matrix, x, y, color1, color2):
def fill(x, y):
if 0 <= x < matWidth and 0 <= y < matHeight and matrix[x][y] == color1:
matrix[x][y] = color2
fill(x - 1, y)
fill(x + 1, y)
fill(x, y - 1)
fill(x, y + 1)
matWidth = len(matrix)
matHeight = len(matrix[0])
fill(x, y)
return matrix

关于python - 需要帮助修改洪水填充算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34998715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com