Skip to the workspace
Pebellslearn by building
Planning DP  ›  Lesson

DP on Grids & the Zone Crossing

DP on Grids 32 minWhen moves only go right and down, the grid is its own table

dsa / DP on Grids & the Zone Crossing

Explore each scene at your own pace.

01 · Counting routes

Counting routes

DP on Grids & the Zone Crossing

Definition
Grid dynamic programming solves a path problem on a grid by letting each cell hold the answer for reaching it, computed from the cells already finished above it and to its left, so the grid itself serves as the table.
In plain words
Picture walking across a city block map that only allows moves right or down. To know how many ways (or what cost) it takes to reach a corner, you only look at the two corners you could have come from, which you have already worked out.

In short: on a grid where the van only moves right or down, every zone's answer depends only on the zone above it and the zone to its left, and both are already finished by the time you get there. So the grid doubles as the table: no separate structure, no recursion, just a fill order where "above" and "left" come first. Swap + for min and the same shape counts routes or finds the cheapest one; closed zones become 0 routes or infinite cost so the combine step quietly ignores them.

![A 3x3 cost grid for [[1,3,1],[1,5,1],[4,2,1]], each cell showing its running cost with arrows from above and left into the bottom-right corner 7, and the winning RRDD path traced in green](/api/assets/dsa/assets/fdab48196afb6ce5.png)

Meridian divides the city into a grid of service zones. Each zone has a congestion toll, and on any given morning a few are closed for roadworks. A van leaving the north-west depot for the south-east one works its way across the grid, and dispatch wants two numbers: how many routes are there, and which one is cheapest?

A grid needs no separate table. If the van only ever moves right or down, every zone can be reached only from the zone above it or the zone to its left, and those two zones already have their answers when you get there. The map is the table, and the only question left is the order you fill it in.

Counting routes

Every route into a zone arrives from above or from the left, and no route does both. So the counts simply add:

ways[r][c] = ways[r - 1][c] + ways[r][c - 1]

A closed zone has zero routes into it, and every count downstream quietly loses the routes that would have used it. Watch a 4 × 5 district fill with two zones closed (X):

watch it move · grid

Every cell on one anti-diagonal (r + c constant) depends only on the diagonal before it, so the answers sweep across the grid as a wavefront from the start corner.

The start has one route, the empty one. The two closed zones will stay at zero routes. 4 by 5 table. At row 0, column 0 — value 1. 3 of 20 cells filled.

1 / 8

The wavefront shows why the fill order is safe: a cell only ever reads cells on the diagonal before it. Row by row is one order that respects this, and column by column is another:

What does this print?
R, C = 3, 3
ways = [[1] * C for _ in range(R)]
for c in range(1, C):          # columns outside,
    for r in range(1, R):      # rows inside
        ways[r][c] = ways[r - 1][c] + ways[r][c - 1]
print(ways[R - 1][C - 1])

The counts grow fast. Guess before you run it:

What does this print?
ways = [[1] * 10 for _ in range(10)]
for r in range(1, 10):
    for c in range(1, 10):
        ways[r][c] = ways[r - 1][c] + ways[r][c - 1]
print(ways[9][9])

You only ever read the row above, so one row is enough. Step through the rolling version, where each row[c] already holds "above" and gains "left":

step through it
1row = [1, 1, 1]
2for r in range(1, 3):
3 for c in range(1, 3):
4 row[c] += row[c - 1]
5print(row)
Scene 1 of 9
Project context and further resources
You're building a piece ofMeridian — the day planner
This piece — cheapest_crossing(): Finds the cheapest way across a grid of service zones, around the closed ones.
Scenario Meridian divides the city into a grid of service zones, each with a congestion toll, and roadworks close some zones for the day. The zone crossing finds the cheapest way for a van to get from the north-west depot to the south-east one, and says which way to turn at every junction.
cheapest_crossing.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
cheapest_crossing(grid) → dictThe cheapest right/down crossing: its total toll and the moves as a string of R and D, or None.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.