Flood fill Algorithm

Data StructureMisc AlgorithmsAlgorithms

One matrix is given; the matrix is representing the one screen. Each element (i, j) of the screen is denoted as a pixel, the color of that pixel is marked with different numbers. In this algorithm, the pixels will be filled with new color when it is already in selected previous color. If the previous color is not the previous color, that pixel will not be filled. After filling a pixel, it will check for its up, down, left and right pixels to do the same.

The idea is really simple, first, we check whether the selected position is colored with the previous color or not, of not, the algorithm will not work. Otherwise, it will fill that pixel with new color and recur for its four neighbors.

Input and Output

Input:
The screen matrix:
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

Output:
Screen matrix after flood fill
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

Algorithm

fillScreen(x, y, prevColor, newColor)

Input: The (x,y) coordinate to start, previous color, and new color.

Output − Screen after changing the color from previous to new, if possible.

Begin
   if (x, y) not in the screen range, then
      return
   if color of (x, y) ≠ prevColor, then
      return
   screen[x, y] := newColor
   fillScreen(x+1, y, prevColor, newColor)
   fillScreen(x-1, y, prevColor, newColor)
   fillScreen(x, y+1, prevColor, newColor)
   fillScreen(x, y-1, prevColor, newColor)
End

Example

#include<iostream>
#define M 8
#define N 8
using namespace std;

int screen[M][N] = {    //the screen dimention and colors
   {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}
};

void fillScreen(int x, int y, int prevColor, int newColor) {    //replace previous color of (x,y), with new color
   if (x < 0 || x >= M || y < 0 || y >= N)    //when point exceeds the screen
      return;

   if (screen[x][y] != prevColor) //if the point(x,y) are not containing prevColor, do nothing
      return;

   screen[x][y] = newColor;    //update the color
   fillScreen(x+1, y, prevColor, newColor);    //for the right of (x,y)
   fillScreen(x-1, y, prevColor, newColor);    //for the left of (x,y)
   fillScreen(x, y+1, prevColor, newColor);    //for the top of (x,y)
   fillScreen(x, y-1, prevColor, newColor);    //for the bottom of (x,y)
}

void floodFill(int x, int y, int newColor) {
   int prevColor = screen[x][y];    //take the color before replacing with new color
   fillScreen(x, y, prevColor, newColor);
}

int main() {
   int x = 4, y = 4, newColor = 3;
   cout << "Previous screen: "<< endl;
   for (int i=0; i<M; i++) {
      for (int j=0; j<N; j++)
         cout << screen[i][j] << " ";
      cout << endl;
   }
   cout << endl;
   floodFill(x, y, newColor);    //start from (4, 4), with new color 3
   
   cout << "Updated screen: "<< endl;
   for (int i=0; i<M; i++) {
      for (int j=0; j<N; j++)
         cout << screen[i][j] << " ";
      cout << endl;
   }
}

Output

Previous 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

Updated 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 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
raja
Published on 12-Jul-2018 08:55:45
Advertisements