Depth-First Search (DFS)
9 min · Kotlin examples
Dive deep along one path before backtracking, great for exploring all reachable nodes.
Kotlin · Depth-First Search (DFS)0/5 cleared
Enemy 1 of 5
This code starts at the top left of a grid and explores every connected 1, marking each one as visited so it never loops. It counts the cells in that group. Press Run to see the count.
Why this works
DFS uses recursion or an explicit stack to go as far as possible, then back up. Mark nodes visited so you don't loop forever.
Counting connected components in a grid is a classic DFS job.
You
Invisible Heisenbug
Loading editor…
Sign up to fightThis one runs on our servers, so it needs a free account.