Flood Fill Algorithm in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
int img[3][3] = {{1,1,0}, {1,0,0}, {1,1,0}}, r = 3, c = 3;
void fill(int x, int y, int old, int new) {
    if (x<0 || y<0 || x>=r || y>=c || img[x][y]!=old) return;
    img[x][y] = new;
    fill(x+1,y,old,new); fill(x-1,y,old,new); fill(x,y+1,old,new); fill(x,y-1,old,new);
}
int main() {
    fill(0,0,1,2);
    for(int i=0;i<r;i++,puts(""))
        for(int j=0;j<c;j++) printf("%d ", img[i][j]);
}

C Output

Input:  
Start: (0,0), Old Color: 1, New Color: 2  

Output:  
2 2 0  
2 0 0  
2 2 0


C++ Program

#include <iostream>
using namespace std;
int img[3][3] = {{1,1,1},{1,1,0},{1,0,1}}, r=3, c=3;
void fill(int x, int y, int old, int n) {
    if(x<0||y<0||x>=r||y>=c||img[x][y]!=old) return;
    img[x][y] = n;
    fill(x+1,y,old,n); fill(x-1,y,old,n); fill(x,y+1,old,n); fill(x,y-1,old,n);
}
int main() {
    fill(1,1,1,9);
    for(int i=0;i<r;i++,cout<<'\n')
        for(int j=0;j<c;j++) cout<<img[i][j]<<" ";
}

C++ Output

Input:  
Start: (1,1), Old Color: 1, New Color: 9 

Output:  
9 9 9  
9 9 0  
9 0 1


JAVA Program

import java.util.*;
class Main {
    static int[][] img = {{0,0,0},{0,1,1},{1,1,0}};
    static void fill(int x, int y, int old, int n) {
        if(x<0||y<0||x>=3||y>=3||img[x][y]!=old) return;
        img[x][y]=n;
        fill(x+1,y,old,n); fill(x-1,y,old,n); fill(x,y+1,old,n); fill(x,y-1,old,n);
    }
    public static void main(String[] args) {
        fill(1,1,1,7);
        for(int[] row : img) System.out.println(Arrays.toString(row));
    }
}

JAVA Output

Input:  
Start: (1,1), Old Color: 1, New Color: 7 

Output:  
[0, 0, 0]  
[0, 7, 7]  
[7, 7, 0]


Python Program

img = [[1,1,1],[1,1,0],[0,0,1]]
def fill(x, y, old, new):
    if x<0 or y<0 or x>=3 or y>=3 or img[x][y]!=old: return
    img[x][y] = new
    for dx,dy in ((1,0),(-1,0),(0,1),(0,-1)): fill(x+dx, y+dy, old, new)
fill(0,0,1,5)
for row in img: print(row)

Python Output

Input:  
Start: (0,0), Old Color: 1, New Color: 5

Output:  
[5, 5, 5]  
[5, 5, 0]  
[0, 0, 1]


In-Depth Explanation
Example
In C++ version, the image is:


1 1 1  
1 1 0  
1 0 1
We start at (1,1) and replace 1 by 9 in all directions that are 4-connected.
It converts all adjacent 1's to 9 until it reaches 0's or the edge.

Real-Life Analogy
Consider a paint bucket tool of MS Paint. When you click at a point, it fills the whole connected area of the same color.
That's flood fill — a restricted spreading of color from a single pixel to all equivalent, surrounding pixels.

Another illustration: spreading fire in a forest simulation, where each surrounding tree is ignited from the central one.

Why It Matters
Flood fill finds application in:

Paint programs and image editors

Region detection in 2D grids

Maze-solving and game maps

Grid-based simulations such as virus spread or game of life

It's also an excellent means to perform DFS/BFS on 2D problems, enhancing insight into connected components.

Learning Insights
Flood fill shows you:

How recursion can spread in 2D space

How to manage spreading using base cases

A different way of viewing depth-first search in a matrix

That matrix/grid problems are disguised graphs

It also reinforces your understanding of problem boundaries, depth of recursion, and space exploration optimization.

Interview & Real-World Use
In interviews, flood fill is included in:

Island counting issues

Coloring maps and regions

Pixel replacement

Zone detecting in simulation

Applied in real-world:

Image processing programs

Medical image analysis

Territory games
Map processing tools

The flood fill algorithm is a fundamental recursive technique for dealing with connected areas within grids. Whether you're implementing color fill in graphics editors, island counting in coding problems, or creating map analyzers for real-world maps, this algorithm assists you in recursively or iteratively accessing all similar, adjacent units. With the brief implementations in C, C++, Java, and Python and simple explanation with real-world applications from this guide, you'll be well prepared to grasp, implement, and explain flood fill professionally in interviews as well as real-world projects.