Pattern visualizer
Flood Fill (Recursion)
This is graph traversal in disguise: each cell is a node, and same-colored neighbors are edges. Recursing outward from the start cell and recoloring as you go — while checking bounds and the original color at every step — naturally stops at any boundary where the color changes, without ever needing to track a separate 'visited' set (the recolor itself IS the visited marker). Animated on: image = [[1,1,1],[1,1,0],[1,0,1]], start=(1,1), newColor=2 — recolor the connected region (shown flattened row-major)..
DFS: recolor, then recurse into all 4 neighbors
Start at (1,1), color=1. Recolor it and every 4-directionally connected cell that shares the same original color.
1FUNCTION floodFill(image, r, c, newColor):2 oldColor = image[r][c]3 FUNCTION dfs(r, c):4 IF (r,c) is out of bounds or image[r][c] != oldColor: RETURN5 image[r][c] = newColor6 dfs into the 4 neighbors: up, down, left, right7 call dfs(r, c)
← / → step · space play · Home restart