需要帮助修改泛洪填充算法

时间:2016-01-25 17:36:20

标签: python recursion flood-fill

我试图修改泛光填充算法以返回最终的2D矩阵,其中所有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

任何帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:1)

只需返回矩阵:

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