📜  洪水填充算法

📅  最后修改于: 2021-04-27 06:14:14             🧑  作者: Mango

给定2D屏幕arr [] [] ,其中每个arr [i] [j]是代表该像素颜色的整数,还给出了像素(X,Y)和颜色C的位置,任务是替换给定像素的颜色以及所有具有给定颜色的相邻相同颜色像素的颜色。

例子:

BFS方法:想法是使用BFS遍历将颜色替换为新颜色。

  • 创建一个空队列,让我们说Q。
  • 按输入中提供的像素推入像素的起始位置,并为其应用替换颜色。
  • 迭代直到Q不为空,然后弹出前节点(像素位置)。
  • 检查与当前像素相邻的像素,如果有效则推入队列(未用替换色上色,并且颜色与旧颜色相同)。

下面是上述方法的实现:

# Python3 implementation of the approach
  
# Function that returns true if 
# the given pixel is valid
def isValid(screen, m, n, x, y, prevC, newC):
    if x<0 or x>= m\
       or y<0 or y>= n or\
       screen[x][y]!= prevC\
       or screen[x][y]== newC:
        return False
    return True
  
  
# FloodFill function
def floodFill(screen,  
            m, n, x,  
            y, prevC, newC):
    queue = []
      
    # Append the position of starting 
    # pixel of the component
    queue.append([x, y])
  
    # Color the pixel with the new color
    screen[x][y] = newC
  
    # While the queue is not empty i.e. the 
    # whole component having prevC color 
    # is not colored with newC color
    while queue:
          
        # Dequeue the front node
        currPixel = queue.pop()
          
        posX = currPixel[0]
        posY = currPixel[1]
          
        # Check if the adjacent
        # pixels are valid
        if isValid(screen, m, n,  
                posX + 1, posY,  
                        prevC, newC):
              
            # Color with newC
            # if valid and enqueue
            screen[posX + 1][posY] = newC
            queue.append([posX + 1, posY])
          
        if isValid(screen, m, n,  
                    posX-1, posY,  
                        prevC, newC):
            screen[posX-1][posY]= newC
            queue.append([posX-1, posY])
          
        if isValid(screen, m, n,  
                posX, posY + 1,  
                        prevC, newC):
            screen[posX][posY + 1]= newC
            queue.append([posX, posY + 1])
          
        if isValid(screen, m, n,  
                    posX, posY-1,  
                        prevC, newC):
            screen[posX][posY-1]= newC
            queue.append([posX, posY-1])
  
  
  
# Driver code
screen =[
[1, 1, 1, 1, 1, 1, 1, 1], 
[1, 1, 1, 1, 1, 1, 0, 0], 
[1, 0, 0, 1, 1, 0, 1, 1], 
[1, 2, 2, 2, 2, 0, 1, 0], 
[1, 1, 1, 2, 2, 0, 1, 0], 
[1, 1, 1, 2, 2, 2, 2, 0], 
[1, 1, 1, 1, 1, 2, 1, 1], 
[1, 1, 1, 1, 1, 2, 2, 1], 
    ]
      
# Row of the display
m = len(screen)
  
# Column of the display
n = len(screen[0])
  
# Co-ordinate provided by the user
x = 4
y = 4
  
# Current colour at that co-ordinate
prevC = screen[x][y]
  
# New colour that has to be filled
newC = 3
  
floodFill(screen, m, n, x, y, prevC, newC)
  
  
# Priting the updated screen
for i in range(m):
    for j in range(n):
        print(screen[i][j], end =' ')
    print()
输出:
1 1 1 1 1 1 1 1 
1 1 1 1 1 1 0 0 
1 0 0 1 1 0 1 1 
1 3 3 3 3 0 1 0 
1 1 1 3 3 0 1 0 
1 1 1 3 3 3 3 0 
1 1 1 1 1 3 1 1 
1 1 1 1 1 3 3 1

DFS方法:同样,DFS方法也可用于实现Flood Fill算法。