Flood Fill in Python

后端 未结 2 1713
有刺的猬
有刺的猬 2020-12-11 08:35

I\'m complitely new to Flood Fill algorithm. I checked it out from Wikipedia (http://en.wikipedia.org/wiki/Flood_fill). But didn\'t become that much wiser. I\'m trying to us

2条回答
  •  天涯浪人
    2020-12-11 08:59

    Well, the idea of flood fill is:

    1. Check if the point meet the criteria.
    2. If it is, change it to "c" (in your case) - and invoke flood fill on all surrounding cells.

    python-like pseudo code:

    def floodfill(matrix, x, y):
        #"hidden" stop clause - not reinvoking for "c" or "b", only for "a".
        if matrix[x][y] == "a":  
            matrix[x][y] = "c" 
            #recursively invoke flood fill on all surrounding cells:
            if x > 0:
                floodfill(matrix,x-1,y)
            if x < len(matrix[y]) - 1:
                floodfill(matrix,x+1,y)
            if y > 0:
                floodfill(matrix,x,y-1)
            if y < len(matrix) - 1:
                floodfill(matrix,x,y+1)
    

提交回复
热议问题