Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1f17076

Browse files
Add Flood Fill Algorithm Implementation (TheAlgorithms#2821)
Co-authored-by: Andrii Siriak <siryaka@gmail.com>
1 parent4fb7470 commit1f17076

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
packagecom.thealgorithms.dynamicprogramming;
2+
3+
/**
4+
* Java program for Flood fill algorithm.
5+
* @author Akshay Dubey (https://github.com/itsAkshayDubey)
6+
*/
7+
publicclassFloodFill {
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+
publicstaticintgetPixel(int[][]image,intx_co_ordinate,inty_co_ordinate) {
18+
19+
returnimage[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+
publicstaticvoidputPixel(int[][]image,intx_co_ordinate,inty_co_ordinate,intnew_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+
publicstaticvoidfloodFill(int[][]image,intx_co_ordinate,inty_co_ordinate,intnew_color,intold_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+
publicstaticvoidprintImageArray(int[][]image) {
71+
72+
for(inti=0 ;i<image.length ;i++) {
73+
for(intj=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+
publicstaticvoidmain(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

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp