The idea is simple, we first replace the color of current pixel, then recur for 4 surrounding points. The following is detailed algorithm.
// A recursive function to replace previous color 'prevC' at '(x, y)' // and all surrounding pixels of (x, y) with new color 'newC' and floodFil(screen[M][N], x, y, prevC, newC)
- If x or y is outside the screen, then return.
- If color of screen[x][y] is not same as prevC, then return
- Recur for north, south, east and west. floodFillUtil(screen, x+1, y, prevC, newC); floodFillUtil(screen, x-1, y, prevC, newC); floodFillUtil(screen, x, y+1, prevC, newC); floodFillUtil(screen, x, y-1, prevC, newC);