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

Commit6be3c99

Browse files
Add Boundary Fill Algorithm (TheAlgorithms#2822)
Co-authored-by: Andrii Siriak <siryaka@gmail.com>
1 parentdee4fc7 commit6be3c99

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
packagecom.thealgorithms.dynamicprogramming;
2+
3+
/**
4+
* Java program for Boundary fill algorithm.
5+
* @author Akshay Dubey (https://github.com/itsAkshayDubey)
6+
*/
7+
publicclassBoundaryFill {
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+
publicstaticintgetPixel(int[][]image,intx_co_ordinate,inty_co_ordinate) {
17+
18+
returnimage[x_co_ordinate][y_co_ordinate];
19+
20+
}
21+
22+
/**
23+
* Put the color at the given co-odrinates of a 2D image
24+
*
25+
* @param image The image to be filed
26+
* @param x_co_ordinate The x co-ordinate at which color is to be filled
27+
* @param y_co_ordinate The y co-ordinate at which color is to be filled
28+
*/
29+
publicstaticvoidputPixel(int[][]image,intx_co_ordinate,inty_co_ordinate,intnew_color) {
30+
31+
image[x_co_ordinate][y_co_ordinate] =new_color;
32+
33+
}
34+
35+
/**
36+
* Fill the 2D image with new color
37+
*
38+
* @param image The image to be filed
39+
* @param x_co_ordinate The x co-ordinate at which color is to be filled
40+
* @param y_co_ordinate The y co-ordinate at which color is to be filled
41+
* @param new_color The new color which to be filled in the image
42+
* @param boundary_color The old color which is to be replaced in the image
43+
*/
44+
publicstaticvoidboundaryFill(int[][]image,intx_co_ordinate,inty_co_ordinate,intnew_color,intboundary_color) {
45+
if(x_co_ordinate >=0 &&y_co_ordinate >=0 &&getPixel(image,x_co_ordinate,y_co_ordinate) !=new_color &&getPixel(image,x_co_ordinate,y_co_ordinate) !=boundary_color) {
46+
47+
putPixel(image,x_co_ordinate,y_co_ordinate,new_color);
48+
boundaryFill(image,x_co_ordinate +1,y_co_ordinate,new_color,boundary_color);
49+
boundaryFill(image,x_co_ordinate -1,y_co_ordinate,new_color,boundary_color);
50+
boundaryFill(image,x_co_ordinate,y_co_ordinate +1,new_color,boundary_color);
51+
boundaryFill(image,x_co_ordinate,y_co_ordinate -1,new_color,boundary_color);
52+
boundaryFill(image,x_co_ordinate +1,y_co_ordinate -1,new_color,boundary_color);
53+
boundaryFill(image,x_co_ordinate -1,y_co_ordinate +1,new_color,boundary_color);
54+
boundaryFill(image,x_co_ordinate +1,y_co_ordinate +1,new_color,boundary_color);
55+
boundaryFill(image,x_co_ordinate -1,y_co_ordinate -1,new_color,boundary_color);
56+
57+
58+
}
59+
60+
}
61+
62+
/**
63+
* This method will print the 2D image matrix
64+
*
65+
* @param image The image to be printed on the console
66+
*/
67+
publicstaticvoidprintImageArray(int[][]image) {
68+
69+
for(inti=0 ;i<image.length ;i++) {
70+
for(intj=0 ;j<image[0].length ;j++) {
71+
72+
System.out.print(image[i][j]+" ");
73+
}
74+
75+
System.out.println();
76+
}
77+
78+
}
79+
80+
// Driver Program
81+
publicstaticvoidmain(String[]args) {
82+
83+
//Input 2D image matrix
84+
int[][]image = {
85+
{0,0,0,0,0,0,0},
86+
{0,3,3,3,3,0,0},
87+
{0,3,0,0,3,0,0},
88+
{0,3,0,0,3,3,3},
89+
{0,3,3,3,0,0,3},
90+
{0,0,0,3,0,0,3},
91+
{0,0,0,3,3,3,3}
92+
};
93+
94+
95+
boundaryFill(image,2,2,5,3);
96+
97+
/* Output ==>
98+
* 0 0 0 0 0 0 0
99+
0 3 3 3 3 0 0
100+
0 3 5 5 3 0 0
101+
0 3 5 5 3 3 3
102+
0 3 3 3 5 5 3
103+
0 0 0 3 5 5 3
104+
0 0 0 3 3 3 3
105+
* */
106+
107+
//print 2D image matrix
108+
printImageArray(image);
109+
}
110+
111+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp