File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ class FloodFill {
2+ public int [][] floodFill (int [][] image , int sr , int sc , int color ) {
3+ int n = image .length ;
4+ int m = image [0 ].length ;
5+ int startingColor = image [sr ][sc ];
6+
7+ if (startingColor == color ){
8+ return image ;
9+ }
10+
11+ Queue <int []> q = new LinkedList <>();
12+
13+ q .add (new int []{sr ,sc });
14+
15+ while (!q .isEmpty ()){
16+ int [] coord = q .remove ();
17+ int row = coord [0 ];
18+ int col = coord [1 ];
19+ image [row ][col ] = color ;
20+ if (row -1 >= 0 && image [row -1 ][col ] == startingColor ){
21+ q .add (new int []{row -1 ,col });
22+ image [row -1 ][col ] = color ;
23+ }
24+ if (row +1 <n && image [row +1 ][col ] == startingColor ){
25+ q .add (new int []{row +1 ,col });
26+ image [row +1 ][col ] = color ;
27+ }
28+ if (col -1 >=0 && image [row ][col -1 ] == startingColor ){
29+ q .add (new int []{row ,col -1 });
30+ image [row ][col -1 ] = color ;
31+ }
32+ if (col +1 <m && image [row ][col +1 ] == startingColor ){
33+ q .add (new int []{row ,col +1 });
34+ image [row ][col +1 ] = color ;
35+ }
36+ }
37+
38+ return image ;
39+ }
40+ }
You can’t perform that action at this time.
0 commit comments