Skip to content

Commit 260a302

Browse files
authored
Fix bound checks in flood fill (fixes TheAlgorithms#2836) (TheAlgorithms#2974)
1 parent 63637c4 commit 260a302

3 files changed

Lines changed: 175 additions & 114 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.thealgorithms.backtracking;
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 The x co-ordinate of which color is to be obtained
14+
* @param y The y co-ordinate of which color is to be obtained
15+
*/
16+
17+
public static int getPixel(int[][] image, int x, int y) {
18+
19+
return image[x][y];
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 The x co-ordinate at which color is to be filled
28+
* @param y The y co-ordinate at which color is to be filled
29+
*/
30+
public static void putPixel(int[][] image, int x, int y, int newColor) {
31+
32+
image[x][y] = newColor;
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 The x co-ordinate at which color is to be filled
42+
* @param y The y co-ordinate at which color is to be filled
43+
* @param newColor The new color which to be filled in the image
44+
* @param oldColor The old color which is to be replaced in the image
45+
* @return
46+
*/
47+
public static void floodFill(int[][] image, int x, int y, int newColor, int oldColor) {
48+
49+
if(x < 0 || x >= image.length) return;
50+
if(y < 0 || y >= image[x].length) return;
51+
if(getPixel(image, x, y) != oldColor) return;
52+
53+
putPixel(image, x, y, newColor);
54+
55+
/* Recursively check for horizontally & vertically adjacent coordinates */
56+
floodFill(image, x + 1, y, newColor, oldColor);
57+
floodFill(image, x - 1, y, newColor, oldColor);
58+
floodFill(image, x, y + 1, newColor, oldColor);
59+
floodFill(image, x, y - 1, newColor, oldColor);
60+
61+
/* Recursively check for diagonally adjacent coordinates */
62+
floodFill(image, x + 1, y - 1, newColor, oldColor);
63+
floodFill(image, x - 1, y + 1, newColor, oldColor);
64+
floodFill(image, x + 1, y + 1, newColor, oldColor);
65+
floodFill(image, x - 1, y - 1, newColor, oldColor);
66+
67+
}
68+
69+
}

src/main/java/com/thealgorithms/dynamicprogramming/FloodFill.java

Lines changed: 0 additions & 114 deletions
This file was deleted.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.thealgorithms.backtracking;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
6+
7+
class FloodFillTest {
8+
9+
@Test
10+
void testForEmptyImage()
11+
{
12+
int image[][] = {};
13+
int expected[][] = {};
14+
15+
FloodFill.floodFill(image, 4, 5, 3, 2);
16+
assertArrayEquals(expected, image);
17+
}
18+
19+
20+
@Test
21+
void testForSingleElementImage()
22+
{
23+
int image[][] = {{1}};
24+
int expected[][] = {{3}};
25+
26+
FloodFill.floodFill(image, 0, 0, 3, 1);
27+
assertArrayEquals(expected, image);
28+
}
29+
30+
31+
@Test
32+
void testForImageOne()
33+
{
34+
int image[][] = {
35+
{ 0,0,0,0,0,0,0 },
36+
{ 0,3,3,3,3,0,0 },
37+
{ 0,3,1,1,5,0,0 },
38+
{ 0,3,1,1,5,5,3 },
39+
{ 0,3,5,5,1,1,3 },
40+
{ 0,0,0,5,1,1,3 },
41+
{ 0,0,0,3,3,3,3 }
42+
};
43+
44+
int expected[][] = {
45+
{ 0,0,0,0,0,0,0 },
46+
{ 0,3,3,3,3,0,0 },
47+
{ 0,3,2,2,5,0,0 },
48+
{ 0,3,2,2,5,5,3 },
49+
{ 0,3,5,5,2,2,3 },
50+
{ 0,0,0,5,2,2,3 },
51+
{ 0,0,0,3,3,3,3 }
52+
};
53+
54+
FloodFill.floodFill(image,2,2,2,1);
55+
assertArrayEquals(expected, image);
56+
}
57+
58+
59+
@Test
60+
void testForImageTwo()
61+
{
62+
int image[][] = {
63+
{ 0,0,1,1,0,0,0 },
64+
{ 1,1,3,3,3,0,0 },
65+
{ 1,3,1,1,5,0,0 },
66+
{ 0,3,1,1,5,5,3 },
67+
{ 0,3,5,5,1,1,3 },
68+
{ 0,0,0,5,1,1,3 },
69+
{ 0,0,0,1,3,1,3 }
70+
};
71+
72+
int expected[][] = {
73+
{ 0,0,2,2,0,0,0 },
74+
{ 2,2,3,3,3,0,0 },
75+
{ 2,3,2,2,5,0,0 },
76+
{ 0,3,2,2,5,5,3 },
77+
{ 0,3,5,5,2,2,3 },
78+
{ 0,0,0,5,2,2,3 },
79+
{ 0,0,0,2,3,2,3 }
80+
};
81+
82+
FloodFill.floodFill(image, 2, 2, 2, 1);
83+
assertArrayEquals(expected, image);
84+
}
85+
86+
87+
@Test
88+
void testForImageThree()
89+
{
90+
int image[][] = {
91+
{ 1,1,2,3,1,1,1 },
92+
{ 1,0,0,1,0,0,1 },
93+
{ 1,1,1,0,3,1,2 }
94+
};
95+
96+
int expected[][] = {
97+
{ 4,4,2,3,4,4,4 },
98+
{ 4,0,0,4,0,0,4 },
99+
{ 4,4,4,0,3,4,2 },
100+
};
101+
102+
FloodFill.floodFill(image, 0, 1, 4, 1);
103+
assertArrayEquals(expected, image);
104+
}
105+
106+
}

0 commit comments

Comments
 (0)