Solution to Leetcode's Flood Fill Question (interesting Question) 
Tags: leetcode, tutorial, algorithms, codenewbie
Published: 2020-05-12 01:14:32

This is an easy [Leetcode 733](https://leetcode.com/problems/flood-fill/) problem. See problem description below:

![problem description]
(https://miro.medium.com/max/1400/1*kgiIrNpaapSP94fIZTuU7w.png)

<h3><b>Question Explanation</b></h3>
To get a clearer picture of what the question wants us to do, first, it will be better to break the multidimensional array elements to form a square-like structure e.g the picture below shows the break down of **[[1,1,1], [1,1,0], [1,0,1]]**:

![splitting array elements]
(https://miro.medium.com/max/966/1*pFLU8Vf5QrykCJObBYjuBg.png)

Now that we have broken the elements into rows and columns as shown in the figure above, the explanation of the question is this; given a particular starting row (**_sr_**) and a starting column(**_sc_**) which is suppose to be the starting pixel and given a new color (**_newColor_**) which is to be used to replace, change that starting pixel (sr, sc) to the new color (**_newColor_**) and also change its neigbours (4-directional to the starting pixel which is of the same colour).

For example, assuming the circled number in the image above is the starting index, the image below shows exactly the steps that will be taken to flood fill:

![flood fill transition steps]
(https://miro.medium.com/max/1206/1*a9XO_frwfRNsVnuiEygP4A.png)

**_NOTE: The colored numbers/pixels are the neigbours (4-directional)of the circled numbers/pixels._**

<h3><b>STEPS</b></h3>
So Step 1 starts from the middle as the starting pixel, changes itself to the new colour which is ‘2’ in this case. It checks its neighbours (left, right, top, bottom), replaces those that had the same number as the one the starting pixel had (which is 1)before it changed to the new colour (2). So it changes all the 1s to 2s as long as they are neighbours. it then moves the ‘batton’ to its left neighbour (1st column)to go through the same process.

Step 2, changes its top and bottom neigbours to 2 because those neighbours had the same number as its initial before itself was changed. **_NOTE that step 2 does not have a left neighbour._**

Step 3 and 4 go through the same process.

if you noticed, the left and right neigbours can be considered as the columns while the top and bottom can be considered as the rows.
ALGORITHM
The first idea that comes to the mind in this kind of problem is **Recursion**.
We need to keep a track of the initial colour and then use recursion to go through all the paths.

The source code is shown below:

```
class Solution {
    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        if (image[sr][sc] == newColor) return image;
        
        flood(image, image[sr][sc], newColor, sr, sc);
        
        return image;
    }
    
public void flood(int[][] image, int color, int newColor, int sr, int sc) {
        if (sr < 0 || sc < 0 ||  sr >= image.length || sc >= image[0].length || image[sr][sc] != color) {
            return;
        }
        
        image[sr][sc] = newColor;
        
        int topRowNeighbour = sr - 1;
        int bottomRowNeighbour = sr + 1;
        int leftColumnNeigbour = sc - 1;
        int rightColumnNeighbour = sc + 1;
        
        flood(image, color, newColor, topRowNeighbour, sc );
        flood(image, color, newColor, bottomRowNeighbour, sc );
        flood(image, color, newColor, sr, leftColumnNeigbour );
        flood(image, color, newColor, sr, rightColumnNeighbour );
    }
}
```
**Thank you very much for reading. Please leave a comment or suggestion.**