


1. Replace the pixel value of ('X', 'Y') with 'P'.
2. Visit all non-diagonal neighbours of ('X', 'Y') having pixel values equal to C and replace their pixel value with 'P'.
3. Non – diagonal neighbours are Up('X' - 1, 'Y'), Down('X' + 1, 'Y'), Left('X', 'Y' - 1), right('X', 'Y' + 1). Also, you cannot go out of bounds.
4. Visit all non-diagonals neighbours of coordinates visited in step 2 having pixel value equal to C and replace their pixel value with 'P'.
5. Repeat step 2, until you have visited all your neighbours
For 'N' = 5 , 'M' = 4 , 'X' = 2 , 'Y' = 2 and 'P' = 5
Given 'IMAGE' is shown below:
[7, 1, 1, 1]
[1, 7, 7, 7]
[7, 7, 7, 0]
[7, 7, 7, 4]
[4, 4, 4, 4]
After the flood fill operation, we will replace all neighbour's 7s with 5.
So our 'IMAGE' will become:
[7, 1, 1, 1]
[1, 5, 5, 5]
[5, 5, 5, 0]
[5, 5, 5, 4]
[4, 4, 4, 4]
The first line contains two space-separated integers 'N' and 'M', denoting the number of rows and columns respectively.
The second line contains three space-separated integers, 'X', 'Y' and 'P', denoting starting coordinates of the pixel and the new pixel value of the flood fill operation.
The next 'N' lines will contain 'M' space-separated integers denoting elements of the image array.
Return a 2D matrix/array of size N * M representing the 'IMAGE' after performing the flood fill operation.
You do not need to print anything, it has already been taken care of. Just implement the given function
In this solution, we will run a dfs starting from a given coordinate ('X', ‘Y’) and visit all its adjacent elements and mark all of them by replacing their pixel value with ‘P’.
The steps are as follows:
void dfs(‘IMAGE’, ‘N’, ‘M’, ‘currentX’, ‘currentY’, ‘P’, ‘C’):
The idea is here to run a BFS starting from a given coordinate ('X', ‘Y’) and visit all elements that are reachable from ('X', ‘Y’).
The steps are as follows:
4. At last, return the ‘IMAGE’.
Sorted Doubly Linked List to Balanced BST
Longest Substring with K-Repeating Characters
Expression Add Operators
Gray Code Transformation
Count of Subsequences with Given Sum