Multi Source Flood Fill Problem
Description
LeetCode Problem 3905.
You are given two integers n and m representing the number of rows and columns of a grid, respectively.
You are also given a 2D integer array sources, where sources[i] = [r_i, c_i, color_i] indicates that the cell (r_i, c_i) is initially colored with color_i. All other cells are initially uncolored and represented as 0.
At each time step, every currently colored cell spreads its color to all adjacent uncolored cells in the four directions: up, down, left, and right. All spreads happen simultaneously.
If multiple colors reach the same uncolored cell at the same time step, the cell takes the color with the maximum value.
The process continues until no more cells can be colored.
Return a 2D integer array representing the final state of the grid, where each cell contains its final color.
Example 1:
Input: n = 3, m = 3, sources = [[0,0,1],[2,2,2]]
Output: [[1,1,2],[1,2,2],[2,2,2]]
Explanation:
The grid at each time step is as follows:

At time step 2, cells (0, 2), (1, 1), and (2, 0) are reached by both colors, so they are assigned color 2 as it has the maximum value among them.
Example 2:
Input: n = 3, m = 3, sources = [[0,1,3],[1,1,5]]
Output: [[3,3,3],[5,5,5],[5,5,5]]
Explanation:
The grid at each time step is as follows:

Example 3:
Input: n = 2, m = 2, sources = [[1,1,5]]
Output: [[5,5],[5,5]]
Explanation:
The grid at each time step is as follows:

Since there is only one source, all cells are assigned the same color.
Constraints:
1 <= n, m <= 10^51 <= n * m <= 10^51 <= sources.length <= n * msources[i] = [r_i, c_i, color_i]0 <= r_i <= n - 10 <= c_i <= m - 11 <= color_i <= 10^6- All
(r_i, c_i)insourcesare distinct.
Solution
Approach: Multi-source BFS
Run a BFS that starts from all source cells at once, one time step per round.
Keep the cells that spread their color in the current round in a list q. At the start, paint every source cell in the answer grid and put it in q.
In each round, use a hash map vis that maps a newly reached cell to the largest color that reaches it in this round. For every cell in q, look at its four neighbors. If a neighbor is still uncolored, set vis[neighbor] = max(vis[neighbor], color). This applies the rule that the maximum color wins when several colors arrive at the same time.
After all cells in q are processed, paint every cell in vis with its color and make these cells the new q. Stop when q is empty, which means nothing more can be colored.
Complexity: time O(nm), space O(nm), where n and m are the grid's rows and columns. Each cell is colored once and looks at four neighbors.
class Solution:
def colorGrid(self, n: int, m: int, sources: List[List[int]]) -> List[List[int]]:
ans = [[0] * m for _ in range(n)]
q = sources
dirs = (-1, 0, 1, 0, -1)
for r, c, color in q:
ans[r][c] = color
while q:
vis = {}
for r, c, color in q:
for i in range(4):
a, b = dirs[i], dirs[i + 1]
x, y = r + a, c + b
if not 0 <= x < n or not 0 <= y < m or ans[x][y]:
continue
vis[(x, y)] = max(vis.get((x, y), 0), color)
q.clear()
for (x, y), color in vis.items():
q.append((x, y, color))
ans[x][y] = color
return ans