Java Program to Implement Flood Fill Algorithm

This is a Java Program to Implement Flood Fill Algorithm. Flood fill, also called seed fill, is an algorithm that determines the area connected to a given node in a multi-dimensional array. It is used in the “bucket” fill tool of paint programs to fill connected, similarly-colored areas with a different color, and in games such as Go and Minesweeper for determining which pieces are cleared. When applied on an image to fill a particular bounded area with color, it is also known as boundary fill. Here ‘P’ is for passage, ‘O’ for obstacle and ‘W’ for water flow.

Here is the source code of the Java Program to Implement Flood Fill Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/**
 * Java Program to Implement Flood Fill Algorithm
 **/
 
import java.util.Scanner;
import java.util.Arrays;
 
/** Class FloodFill **/
public class FloodFill
{
    /** Function to fill grid **/
    private void fillGrid(char[][] arr, int r, int c) 
    {
        if (arr[r] == 'P')
        {
            arr[r] = 'W';
            display(arr);
 
            fillGrid(arr, r + 1, c);
            fillGrid(arr, r - 1, c);
            fillGrid(arr, r, c + 1);
            fillGrid(arr, r, c - 1);
        }
    }
    /** Function to display grid **/
    private void display(char[][] arr)
    {
        System.out.println("\nGrid : ");
        for (int i = 1; i < arr.length - 1; i++)
        {
            for (int j = 1; j < arr[i].length - 1; j++)
                System.out.print(arr[i][j] +" ");
            System.out.println();
        }
    }
 
    /** Main method **/
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner( System.in );        
        System.out.println("Flood Fill Test\n");
 
        /** Accept dimensions **/
        System.out.println("Enter dimensions of grid");
        int M = scan.nextInt();
        int N = scan.nextInt();
 
        /** make grid with border as obstacle to avoid boundary conditions **/
        char[][] arr = new char[M + 2][N + 2];
        for (int i = 0; i < M + 2; i++)
            Arrays.fill(arr[i], 'O');
 
        /** Accept grid **/
        System.out.println("Enter grid with 'P' for passage and 'O' for obstacle");
 
        for (int i = 1; i < M + 1; i++)
            for (int j = 1; j < N + 1; j++)
                arr[i][j] = scan.next().charAt(0);    
 
        System.out.println("Enter coordinates to start ");
        int sr = scan.nextInt();
        int sc = scan.nextInt();
 
        if (arr[sr][sc] != 'P')
        {
            System.out.println("Invalid coordinates");
            System.exit(0);
        }
 
        FloodFill ff = new FloodFill();
        ff.fillGrid(arr, sr, sc);    
 
    }    
}
Flood Fill Test
 
Enter dimensions of grid
5 5
Enter grid with 'P' for passage and 'O' for obstacle
P P P P P
O P O P O
O O P P O
P P P O O
P O O O O
Enter coordinates to start
3 3
 
Grid :
P P P P P
O P O P O
O O W P O
P P P O O
P O O O O
 
Grid :
P P P P P
O P O P O
O O W P O
P P W O O
P O O O O
 
Grid :
P P P P P
O P O P O
O O W P O
P W W O O
P O O O O
 
Grid :
P P P P P
O P O P O
O O W P O
W W W O O
P O O O O
 
Grid :
P P P P P
O P O P O
O O W P O
W W W O O
W O O O O
 
Grid :
P P P P P
O P O P O
O O W W O
W W W O O
W O O O O
 
Grid :
P P P P P
O P O W O
O O W W O
W W W O O
W O O O O
 
Grid :
P P P W P
O P O W O
O O W W O
W W W O O
W O O O O
 
Grid :
P P P W W
O P O W O
O O W W O
W W W O O
W O O O O
 
Grid :
P P W W W
O P O W O
O O W W O
W W W O O
W O O O O
 
Grid :
P W W W W
O P O W O
O O W W O
W W W O O
W O O O O
 
Grid :
P W W W W
O W O W O
O O W W O
W W W O O
W O O O O
 
Grid :
W W W W W
O W O W O
O O W W O
W W W O O
W O O O O

Related posts:

Java Program to Implement Dijkstra’s Algorithm using Queue
Generate Spring Boot REST Client with Swagger
How to Round a Number to N Decimal Places in Java
Lớp TreeMap trong Java
Request a Delivery / Read Receipt in Javamail
Dockerizing a Spring Boot Application
Concurrent Test Execution in Spring 5
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Hướng dẫn sử dụng Java Annotation
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Java toString() Method
Java Program to Implement ScapeGoat Tree
Convert Character Array to String in Java
Arrays.asList vs new ArrayList(Arrays.asList())
Converting Between Byte Arrays and Hexadecimal Strings in Java
Static Content in Spring WebFlux
Java Program to Implement Shunting Yard Algorithm
Java Program to Find a Good Feedback Vertex Set
Guide to CopyOnWriteArrayList
A Guide to Apache Commons Collections CollectionUtils
Hướng dẫn Java Design Pattern – Decorator
Wiring in Spring: @Autowired, @Resource and @Inject
Xử lý ngoại lệ trong Java (Exception Handling)
Java Program to Solve TSP Using Minimum Spanning Trees
Java Program to Implement Sieve Of Atkin
Java Program to Implement ConcurrentSkipListMap API
Lập trình đa luồng với Callable và Future trong Java
Logging in Spring Boot
Base64 encoding và decoding trong Java 8
Java Program to Perform the Unique Factorization of a Given Number
Hướng dẫn Java Design Pattern – Dependency Injection