1
+ package com .thealgorithms .dynamicprogramming ;
2
+
3
+ /**
4
+ * Java program for Flood fill algorithm.
5
+ * @author Akshay Dubey (https://github.com/itsAkshayDubey)
6
+ */
7
+ public class FloodFill {
8
+
9
+ /**
10
+ * Get the color at the given co-odrinates of a 2D image
11
+ *
12
+ * @param image The image to be filled
13
+ * @param x_co_ordinate The x co-ordinate of which color is to be obtained
14
+ * @param y_co_ordinate The y co-ordinate of which color is to be obtained
15
+ */
16
+
17
+ public static int getPixel (int [][] image , int x_co_ordinate , int y_co_ordinate ) {
18
+
19
+ return image [x_co_ordinate ][y_co_ordinate ];
20
+
21
+ }
22
+
23
+ /**
24
+ * Put the color at the given co-odrinates of a 2D image
25
+ *
26
+ * @param image The image to be filed
27
+ * @param x_co_ordinate The x co-ordinate at which color is to be filled
28
+ * @param y_co_ordinate The y co-ordinate at which color is to be filled
29
+ */
30
+ public static void putPixel (int [][] image , int x_co_ordinate , int y_co_ordinate , int new_color ) {
31
+
32
+ image [x_co_ordinate ][y_co_ordinate ] = new_color ;
33
+
34
+ }
35
+
36
+
37
+ /**
38
+ * Fill the 2D image with new color
39
+ *
40
+ * @param image The image to be filed
41
+ * @param x_co_ordinate The x co-ordinate at which color is to be filled
42
+ * @param y_co_ordinate The y co-ordinate at which color is to be filled
43
+ * @param new_color The new color which to be filled in the image
44
+ * @param old_color The old color which is to be replaced in the image
45
+ * @return
46
+ */
47
+ public static void floodFill (int [][] image , int x_co_ordinate , int y_co_ordinate , int new_color , int old_color ) {
48
+ if (x_co_ordinate >= 0 && y_co_ordinate >= 0 && getPixel (image , x_co_ordinate , y_co_ordinate ) == old_color ) {
49
+
50
+ putPixel (image , x_co_ordinate , y_co_ordinate , new_color );
51
+ floodFill (image , x_co_ordinate + 1 , y_co_ordinate , new_color , old_color );
52
+ floodFill (image , x_co_ordinate - 1 , y_co_ordinate , new_color , old_color );
53
+ floodFill (image , x_co_ordinate , y_co_ordinate + 1 , new_color , old_color );
54
+ floodFill (image , x_co_ordinate , y_co_ordinate - 1 , new_color , old_color );
55
+ floodFill (image , x_co_ordinate + 1 , y_co_ordinate - 1 , new_color , old_color );
56
+ floodFill (image , x_co_ordinate - 1 , y_co_ordinate + 1 , new_color , old_color );
57
+ floodFill (image , x_co_ordinate + 1 , y_co_ordinate + 1 , new_color , old_color );
58
+ floodFill (image , x_co_ordinate - 1 , y_co_ordinate - 1 , new_color , old_color );
59
+
60
+
61
+ }
62
+
63
+ }
64
+
65
+ /**
66
+ * This method will print the 2D image matrix
67
+ *
68
+ * @param image The image to be printed on the console
69
+ */
70
+ public static void printImageArray (int [][] image ) {
71
+
72
+ for (int i =0 ; i <image .length ; i ++) {
73
+ for (int j =0 ; j <image [0 ].length ; j ++) {
74
+
75
+ System .out .print (image [i ][j ]+" " );
76
+ }
77
+
78
+ System .out .println ();
79
+ }
80
+
81
+ }
82
+
83
+ // Driver Program
84
+ public static void main (String [] args ) {
85
+
86
+
87
+ //Input 2D image matrix
88
+ int [][] image = {
89
+ {0 ,0 ,0 ,0 ,0 ,0 ,0 },
90
+ {0 ,3 ,3 ,3 ,3 ,0 ,0 },
91
+ {0 ,3 ,1 ,1 ,5 ,0 ,0 },
92
+ {0 ,3 ,1 ,1 ,5 ,5 ,3 },
93
+ {0 ,3 ,5 ,5 ,1 ,1 ,3 },
94
+ {0 ,0 ,0 ,5 ,1 ,1 ,3 },
95
+ {0 ,0 ,0 ,3 ,3 ,3 ,3 }
96
+ };
97
+
98
+ floodFill (image ,2 ,2 ,2 ,1 );
99
+
100
+ /* Output ==>
101
+ * 0 0 0 0 0 0 0
102
+ 0 3 3 3 3 0 0
103
+ 0 3 2 2 5 0 0
104
+ 0 3 2 2 5 5 3
105
+ 0 3 5 5 2 2 3
106
+ 0 0 0 5 2 2 3
107
+ 0 0 0 3 3 3 3
108
+ * */
109
+
110
+ //print 2D image matrix
111
+ printImageArray (image );
112
+ }
113
+
114
+ }
0 commit comments