Loading problem...
You are given a 2D grid representing a canvas where each cell contains an integer representing its color value. You are also provided with a starting cell position defined by its row index sr and column index sc, along with a new color value newColor.
Your task is to perform a region color propagation operation starting from the cell at position (sr, sc). This operation works as follows:
newColor.newColor.Return the modified grid after the propagation is complete.
Note: Diagonal cells are not considered adjacent in this problem — only cells sharing a horizontal or vertical edge are connected.
image = [[1,1,1],[1,1,0],[1,0,1]]
sr = 1
sc = 1
color = 2[[2,2,2],[2,2,0],[2,0,1]]Starting from cell (1, 1) which has color 1, we propagate the new color 2 to all connected cells with the same original color 1. The cells at positions (0,0), (0,1), (0,2), (1,0), (1,1), and (2,0) are all connected through horizontal or vertical adjacency and share the original color 1, so they all become color 2. The cell at position (2,2) has color 1 but is NOT connected to the starting cell because the path is blocked by cells with different colors (0s), so it remains unchanged.
image = [[0,0,0],[0,0,0]]
sr = 0
sc = 0
color = 0[[0,0,0],[0,0,0]]The starting cell at (0, 0) already has the target color 0. Since the original color and the new color are identical, no visual change occurs in the grid, and it remains as is.
image = [[1,2,1],[2,2,2],[1,2,1]]
sr = 1
sc = 1
color = 3[[1,3,1],[3,3,3],[1,3,1]]Starting from cell (1, 1) with original color 2, we propagate color 3 to all orthogonally connected cells with color 2. This forms a cross pattern: (0,1), (1,0), (1,1), (1,2), and (2,1). The corner cells remain color 1 since they are not connected to the region of color 2.
Constraints