Monday 27 November 2017

Java Program to Implement Flood Fill Algorithm


Code:

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][c] == 'P')
        {
            arr[r][c] = '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);    

    }    
}


Output:

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



More Java Programs:


















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...