AlgoViz

Grid backtracking

medium 25 min

Grid backtracking is depth-first search on a 2D board where the visited mark belongs to the current path: mark a cell on the way in, explore its neighbours, unmark it on the way out so other paths can use it.

Where it shows up

The same pattern appears across everyday software.

  • Word games and puzzle apps

    Boggle and word-search apps find every dictionary word on a letter grid by walking neighbouring cells without reuse. They store the dictionary in a trie so a whole branch of the search is dropped the moment no word starts with the letters so far.

  • Sudoku and logic puzzle solvers

    Puzzle generators and solvers fill cells with constraint checks and undo on conflict. Generators also use the solver to make sure a new puzzle has exactly one solution, by searching for a second one.

  • Robot path planning in tight spaces

    When a robot or game character must visit every cell of a room once (cleaning, inspection, a puzzle level), the planner searches paths on a grid map, marking cells as it plans and unmarking them when a plan fails.

  • Chip layout and wire routing

    Placing components and routing wires on a grid so that nothing overlaps is a constraint search: claim cells for a wire, check the rules, and release them when a later wire cannot fit. Real routers add heuristics, but the claim and release step is the same.

  • Polymer and protein models

    Physicists model a polymer chain as a self-avoiding walk on a lattice, a path that never revisits a cell. Small cases are counted exactly by the same mark, explore, unmark enumeration used for maze paths.