Fix : Floodfill infinite recursion due to same color (#4359)

Fix : Floodfill infinite recursion due to same color
This commit is contained in:
Manan Solanki 2023-09-09 23:37:59 +05:30 committed by GitHub
parent 81f38174a6
commit a88abb7ac2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View File

@ -39,6 +39,7 @@ public class FloodFill {
* @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;

View File

@ -93,4 +93,14 @@ class FloodFillTest {
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);
}
}