Skip to content
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);
}
}