Repository: TheAlgorithms/Java (java)

Issue:
[BUG] Backtracking > Floodfill : Code goes in infinite calling loop when same color is passed in newColor and oldColor
### Description

Backtracking > Floodfill 
Hi,
When same int color  is passed as both newColor and oldColor parameter, code goes to infinite calling loop in recursion and stack overflow exception occurs causing program to shut down.


My observation is , this case is not handled properly in base cases and 
I would really like contributing to resolve this issue. 
Here is my PR , resolving this issue : https://github.com/TheAlgorithms/Java/pull/4359
Thanks.


### Steps to reproduce

Pass same color as newColor and oldColor as example while calling FloodFill.floodFill method.

```
Testcase : 
@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, 0, 1, 1); // newColor = oldColor = 1
        assertArrayEquals(expected, image);
    }
```

This testcase produce error as below : 
```
java.lang.StackOverflowError
	at com.thealgorithms.backtracking.FloodFill.floodFill(FloodFill.java:49)
	at com.thealgorithms.backtracking.FloodFill.floodFill(FloodFill.java:49)
	at com.thealgorithms.backtracking.FloodFill.floodFill(FloodFill.java:50)
	at com.thealgorithms.backtracking.FloodFill.floodFill(FloodFill.java:49)
	at com.thealgorithms.backtracking.FloodFill.floodFill(FloodFill.java:50)
	...
```


### Excepted behavior

As both new and old color are same it should not change anything in given image and return from the function without any exception or issue.

### Screenshots

_No response_

### Additional context

_No response_

Pull request:
#4358 Fix : Floodfill infinite recursion due to same color
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [ ] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.

Fixed #4358 
Fixed case when both newColor and oldColor is passed as same integer causing infinite recursion by adding extra base condition.
Also added testcase for same and after that ran all testcase, which resulted as all passed.

<img width="1511" alt="Screenshot 2023-09-09 at 6 04 36 PM" src="https://github.com/TheAlgorithms/Java/assets/76104205/b046b071-d9c3-443b-83c4-16075332724c">



Patch:
diff --git a/src/main/java/com/thealgorithms/backtracking/FloodFill.java b/src/main/java/com/thealgorithms/backtracking/FloodFill.java
index 01b26916..eb4939b3 100644
--- a/src/main/java/com/thealgorithms/backtracking/FloodFill.java
+++ b/src/main/java/com/thealgorithms/backtracking/FloodFill.java
@@ -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;


Test patch:
diff --git a/src/test/java/com/thealgorithms/backtracking/FloodFillTest.java b/src/test/java/com/thealgorithms/backtracking/FloodFillTest.java
index 7a59b210..8b860b5b 100644
--- a/src/test/java/com/thealgorithms/backtracking/FloodFillTest.java
+++ b/src/test/java/com/thealgorithms/backtracking/FloodFillTest.java
@@ -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);
+    }
 }