Skip to content

#4358 Fix : Floodfill infinite recursion due to same color #4359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static void putPixel(int[][] image, int x, int y, int newColor) {
* @param oldColor The old color which is to be replaced in the image
*/
public static void floodFill(int[][] image, int x, int y, int newColor, int oldColor) {
if (newColor == oldColor) return;
if (x < 0 || x >= image.length) return;
if (y < 0 || y >= image[x].length) return;
if (getPixel(image, x, y) != oldColor) return;
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/thealgorithms/backtracking/FloodFillTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,14 @@ void testForImageThree() {
FloodFill.floodFill(image, 0, 1, 4, 1);
assertArrayEquals(expected, image);
}

@Test
void testForSameNewAndOldColor() {
int[][] image = {{1, 1, 2}, {1, 0, 0}, {1, 1, 1}};

int[][] expected = {{1, 1, 2}, {1, 0, 0}, {1, 1, 1}};

FloodFill.floodFill(image, 0, 1, 1, 1);
assertArrayEquals(expected, image);
}
}