Skip to content

Commit a88abb7

Browse files
authored
Fix : Floodfill infinite recursion due to same color (#4359)
Fix : Floodfill infinite recursion due to same color
1 parent 81f3817 commit a88abb7

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public static void putPixel(int[][] image, int x, int y, int newColor) {
3939
* @param oldColor The old color which is to be replaced in the image
4040
*/
4141
public static void floodFill(int[][] image, int x, int y, int newColor, int oldColor) {
42+
if (newColor == oldColor) return;
4243
if (x < 0 || x >= image.length) return;
4344
if (y < 0 || y >= image[x].length) return;
4445
if (getPixel(image, x, y) != oldColor) return;

src/test/java/com/thealgorithms/backtracking/FloodFillTest.java

+10
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,14 @@ void testForImageThree() {
9393
FloodFill.floodFill(image, 0, 1, 4, 1);
9494
assertArrayEquals(expected, image);
9595
}
96+
97+
@Test
98+
void testForSameNewAndOldColor() {
99+
int[][] image = {{1, 1, 2}, {1, 0, 0}, {1, 1, 1}};
100+
101+
int[][] expected = {{1, 1, 2}, {1, 0, 0}, {1, 1, 1}};
102+
103+
FloodFill.floodFill(image, 0, 1, 1, 1);
104+
assertArrayEquals(expected, image);
105+
}
96106
}

0 commit comments

Comments
 (0)