Flood Fill Algorithm
Given a 2D screen arr[][] where each arr[i][j] is an integer representing the color of that pixel, also given the location of a pixel (X, Y) and a color C, the task is to replace the color of the given pixel and all the adjacent same-colored pixels with the given color.
Example:
Input: arr[][] = {
{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}}
X = 4, Y = 4, C = 3
Output:
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
Explanation:
The values in the given 2D screen indicate colors of the pixels. X and Y are coordinates of the brush, C is the color that should replace the previous color on screen[X][Y] and all surrounding pixels with the same color. Hence all the 2 are replaced with 3.
BFS Approach: The idea is to use BFS traversal to replace the color with the new color.
- Create an empty queue lets say Q.
- Push the starting location of the pixel as given in the input and apply replacement color to it.
- Iterate until Q is not empty and pop the front node (pixel position).
- Check the pixels adjacent to the current pixel and push into the queue if valid (had not been colored with replacement color and have the same color as the old color).
Below is the implementation of the above approach:
Python3
# Python3 implementation of the approach# Function that returns true if# the given pixel is validdef 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 functiondef 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 codescreen =[[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 displaym = len(screen)# Column of the displayn = len(screen[0])# Co-ordinate provided by the userx = 4y = 4# Current color at that co-ordinateprevC = screen[x][y]# New color that has to be fillednewC = 3floodFill(screen, m, n, x, y, prevC, newC)# Printing the updated screenfor 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 Approach: Similarly DFS approach can be used to implement the Flood Fill algorithm as well.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


