The question
A robot starts on the top-left square of a 3 by 4 grid and must reach the bottom-right square. Each move goes one square right or one square down. How many different routes can it take?
Every route is 5 moves: 3 right and 2 down, in some order. Listing them works for this small grid, but the list explodes: a 16 by 16 grid has 155,117,520 routes. We need to count without listing.
Fill the table
Any route into a square arrives from the square above it or from the square to its left. So ways(r, c) = ways(above) + ways(left). That is the state and the transition: each count needs only two smaller counts.
The start holds 1: the route that has not moved yet. A top-row square can only be reached from the left, and a left-column square only from above, so each of them holds 1 too.
Go left to right. (1, 1) = 1 above + 1 left = 2. Then (1, 2) = 1 + 2 = 3, and (1, 3) = 1 + 3 = 4. Both numbers a square needs are always filled already.
Same again: 2 + 1 = 3, then 3 + 3 = 6, then 4 + 6 = 10. The goal holds 10: there are 10 routes. We did one small sum per square, not one walk per route.
Rocks and cost
Check another way. A route is 5 moves, and picking which 2 of them go down fixes the route: 5 choose 2 = 10. But the formula needs an empty grid.
Now a rock blocks square (1, 1). No route may pass through it, so it holds 0. The formula no longer fits, but the table still works: line 6 skips the rock and leaves its count at 0.
Same order, same sums. Row 1: 1 + 0 = 1, 1 + 1 = 2. Row 2: 0 + 1 = 1, 1 + 1 = 2, and the goal 2 + 2 = 4. Only 4 of the 10 routes dodge the rock.
The loops visit each square once and do at most two additions there, so an R by C grid costs O(R × C) time. A 16 by 16 grid is 256 squares, against 155,117,520 routes to list.
What happens
;; step 1/10 · routes grid: 3 rows × 4 columns, squares (row, column) start (0, 0), goal (2, 3) moves: right (column + 1) or down (row + 1)
Each square needs the square above and the square to its left. Filling row by row, left to right, always has both ready. In MIT 6.006 terms that is a topological order of the subproblems: they form a DAG, so a plain loop can solve them bottom up. With no rocks, an R by C grid has C(R + C - 2, R - 1) routes: R + C - 2 moves, choose which R - 1 go down. Python 3.8 and later give it as math.comb; math.comb(30, 15) is 155,117,520. One rock breaks the formula, but not the table.