Practice Discrete Math

Algorithms / Grid Graph Traversal

Least You Need to Know: Grid Traversal, Flood Fill, and Boundary Checks

Many interview grid problems are just implicit graph traversal: each cell is a node, legal moves define edges, and BFS or DFS visits connected components or shortest paths. The main implementation risk is careful neighbor and boundary handling.

The least you need to know

Key notation

(r, c) row and column state
4-neighbors up, down, left, right
component maximal connected region under the movement rule

Tiny worked example

  • To count islands, scan the grid for an unvisited land cell.
  • Each time you find one, start DFS or BFS and mark its whole connected land region.
  • That one traversal claims exactly one island.
  • Continue scanning until all land cells are assigned to some traversal.

Common mistakes

How to recognize this kind of problem

Start practice