Flood fill Algorithm | Pair class ; Function to check valid coordinate ; Function to run bfs ; Visiing array ; Initialing all as zero ; Creating queue for bfs ; Pushing pair of { x , y } ; Marking { x , y } as visited ; Untill queue is emppty ; Extrating front pair ; Poping front pair of queue ; For Upside Pixel or Cell ; For Downside Pixel or Cell ; For Right side Pixel or Cell ; For Left side Pixel or Cell ; Printing The Changed Matrix Of Pixels ; Driver Code ; Function Call
class Pair implements Comparable < Pair > { int first ; int second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } @ Override public int compareTo ( Pair o ) { return second - o . second ; } } class GFG { public static int validCoord ( int x , int y , int n , int m ) { if ( x < 0 y < 0 ) { return 0 ; } if ( x >= n y >= m ) { return 0 ; } return 1 ; } public static void bfs ( int n , int m , int data [ ] [ ] , int x , int y , int color ) { int vis [ ] [ ] = new int [ 101 ] [ 101 ] ; for ( int i = 0 ; i <= 100 ; i ++ ) { for ( int j = 0 ; j <= 100 ; j ++ ) { vis [ i ] [ j ] = 0 ; } } Queue < Pair > obj = new LinkedList < > ( ) ; Pair pq = new Pair ( x , y ) ; obj . add ( pq ) ; vis [ x ] [ y ] = 1 ; while ( ! obj . isEmpty ( ) ) { Pair coord = obj . peek ( ) ; int x1 = coord . first ; int y1 = coord . second ; int preColor = data [ x1 ] [ y1 ] ; data [ x1 ] [ y1 ] = color ; obj . remove ( ) ; if ( ( validCoord ( x1 + 1 , y1 , n , m ) == 1 ) && vis [ x1 + 1 ] [ y1 ] == 0 && data [ x1 + 1 ] [ y1 ] == preColor ) { Pair p = new Pair ( x1 + 1 , y1 ) ; obj . add ( p ) ; vis [ x1 + 1 ] [ y1 ] = 1 ; } if ( ( validCoord ( x1 - 1 , y1 , n , m ) == 1 ) && vis [ x1 - 1 ] [ y1 ] == 0 && data [ x1 - 1 ] [ y1 ] == preColor ) { Pair p = new Pair ( x1 - 1 , y1 ) ; obj . add ( p ) ; vis [ x1 - 1 ] [ y1 ] = 1 ; } if ( ( validCoord ( x1 , y1 + 1 , n , m ) == 1 ) && vis [ x1 ] [ y1 + 1 ] == 0 && data [ x1 ] [ y1 + 1 ] == preColor ) { Pair p = new Pair ( x1 , y1 + 1 ) ; obj . add ( p ) ; vis [ x1 ] [ y1 + 1 ] = 1 ; } if ( ( validCoord ( x1 , y1 - 1 , n , m ) == 1 ) && vis [ x1 ] [ y1 - 1 ] == 0 && data [ x1 ] [ y1 - 1 ] == preColor ) { Pair p = new Pair ( x1 , y1 - 1 ) ; obj . add ( p ) ; vis [ x1 ] [ y1 - 1 ] = 1 ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { System . out . print ( data [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int nn , mm , xx , yy , colorr ; nn = 8 ; mm = 8 ; int dataa [ ] [ ] = { { 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 } , } ; xx = 4 ; yy = 4 ; colorr = 3 ; bfs ( nn , mm , dataa , xx , yy , colorr ) ; } }