INAIO Guide Stage 1 12Graph Theory and Algorithms
12.4 Grid paths and constraints
Treat a grid as a graph, then add keys or resources to the search state when they change future moves.
A grid is a special kind of graph. Each usable cell is a vertex, and each permitted move between neighbouring cells is an edge. The rows and columns tell us where the vertices are, so we can generate their neighbours without drawing every edge. Once we have this graph, the BFS and DFS methods from Module 12.3 work just as they do on a network of named circles: BFS finds the fewest equal-cost moves, while DFS can test whether a route exists.
The grid adds rules that affect which edges exist. A wall blocks a cell or move. A locked door may be passable only after collecting a key. If future moves depend on a key, remaining energy, or direction, the search must remember that information along with the cell. This is intermediate graph search built on coordinates from Module 10.1. MIT OpenCourseWare's graph-search lecture supplies the search method; the grid supplies its neighbours.
A basic idea: a movement rule creates the edges
For a four-neighbour grid, a cell can move to , , , or when that destination is inside the grid and open. These are exactly its graph neighbours. Eight-neighbour movement also allows diagonals. State which convention the question uses: a diagonal changes both reachability and shortest-route length. Also say whether a diagonal may pass between blocked cells if the problem has that rule.
A quick tour of movement constraints
For an unweighted grid, BFS finds the fewest moves because every legal move costs one edge. Store the start, a queue, a visited set, and the parent of each discovered state. Check boundaries and walls before adding a neighbour. DFS can answer whether a route exists but its first route may not be shortest.
The key question is what makes two visits equivalent? If you can carry a key, the state is , not just . Reaching a cell with the key can enable moves that were impossible on an earlier visit without it. Likewise, with a limited number of wall breaks, store the number used or remaining. Marking only the cell as visited can discard a valid route.
If moves have unequal costs, ordinary BFS no longer minimizes total cost. A question about the fewest turns may need direction in the state as well: . Choose a state containing exactly the facts needed to decide future legal moves and cost.
Application 1: shortest route around a wall
In a by grid, the centre cell is blocked. You may move up, down, left, or right. What is the minimum number of moves from to ?
- Find a lower bound. The row must increase twice and the column twice, so at least moves are needed.
- Give a valid route. uses moves and avoids the centre.
- Conclude. The lower bound is met, so is the minimum. BFS would find one of the edge routes in its fourth layer.
Application 2: a key before a door
The pictured grid has top row , then a row with one open cell below and walls elsewhere, then below that open cell and walls elsewhere. The door opens only after collecting the key . What is the shortest four-neighbour route?
- Go to the key. From , move to and : two moves.
- Return and finish. Move back through and , then right through , , and : five more moves, seven total.
- Explain the state. The second visit to has , unlike the first visit. BFS must treat them as distinct states to use the door.
The walls make the door the only route to the goal.
Resources
| Source | Title | Why read it |
|---|---|---|
| MIT OpenCourseWare | Searching II: Breadth-First Search and Depth-First Search | Apply graph search after representing allowed grid moves as edges. |
Finished this module? Mark it done.
Comments
Stuck on something, spotted a mistake, or found a resource that helped you? Tell us below.
Loading comments