Active tool:Paint
Click any cell to start Flood Fill 🎨
Draw walls first, then paint to see the fill stop at walls
About Flood Fill
Flood Fill works like the bucket tool in paint apps. Starting from a point, it spreads in 4 directions, coloring every matching/empty cell until hitting a wall or grid edge. Can be implemented with BFS (queue) or DFS (stack/recursive).
Time
O(R×C)
Space
O(R×C)
Use
Paint apps
✓ 2D grid✓ BFS or DFS✓ Walls stop fill
flood_fill.py
from collections import deque
def flood_fill(grid, sr, sc, color):
rows, cols = len(grid), len(grid[0])
target = grid[sr][sc]
if target == color: return
queue = deque([(sr, sc)])
grid[sr][sc] = color # رنگ مبدأ
dirs = [(0,1),(0,-1),(1,0),(-1,0)]
while queue:
r, c = queue.popleft()
for dr, dc in dirs:
nr, nc = r+dr, c+dc
if 0<=nr<rows and 0<=nc<cols:
if grid[nr][nc] == target:
grid[nr][nc] = color
queue.append((nr, nc))
# O(R × C) — هر خانه یکبار بازدید میشود