Grid Traversal

Grid traversal: treating a grid of cells as a graph for breadth-first and depth-first search, flood fills and searches from many starting cells.

  • 8 cards
  • 4 questions
  • About 24 min
  • Intermediate to Advanced
Share

Grid traversal treats a map of cells as a graph whose vertices are the cells and whose edges join each cell to the ones beside it, so the graph searches of the Graph Traversal topic in Algorithms run on the grid without building anything. It matters because grids hide in many questions, as floors, maps, boards and floor plans, and a search that tries routes one by one, or runs once per cell, repeats work that one wave, one fill or one many-source search does once. The topic opens on a breadth-first search spreading over a small grid wave by wave, finds the fewest moves across a warehouse floor without trying every route, and gives the grid search template in Python, Java and TypeScript. It counts islands with a flood fill, finds every desk's nearest exit with one search from all the exits, shows what marking a cell when it leaves the queue costs, and ends with an exercise on counting enclosed courtyards.

Contents

8 cardsnot started