Grid Min Path Sum
Cheapest delivery route across an 8×8 cost grid, moving only right or down.
A1:H8 is a per-cell toll cost for a delivery drone crossing a city grid.
Each cell holds the dollars charged for entering that cell. The drone enters
at A1 (top-left) and must reach H8 (bottom-right), moving only right
or down at each step. It pays the toll for every cell it visits, including
the start and the end.
Output the minimum total toll across all valid routes.
There are C(14,7) = 3,432 distinct routes through this grid. Brute force is
not happening by hand. The trick in the formula is the out-of-bounds sentinel:
returning 0 makes invalid paths look appealing, so the recursion has to
return a value larger than any real path so MIN ignores them.
Input
Range A1:H8
| A | B | C | D | E | F | G | H | |
|---|---|---|---|---|---|---|---|---|
| 1 | 19 | 7 | 18 | 12 | 15 | 2 | 17 | 7 |
| 2 | 20 | 8 | 10 | 16 | 7 | 11 | 15 | 3 |
| 3 | 9 | 7 | 6 | 3 | 15 | 18 | 12 | 7 |
| 4 | 16 | 11 | 8 | 13 | 14 | 7 | 3 | 14 |
| 5 | 15 | 10 | 7 | 4 | 20 | 11 | 9 | 13 |
| 6 | 13 | 8 | 14 | 3 | 13 | 14 | 1 | 15 |
| 7 | 7 | 18 | 4 | 12 | 1 | 12 | 12 | 1 |
| 8 | 9 | 19 | 5 | 2 | 14 | 6 | 9 | 3 |
Hint
Branching recursion with MIN. Out-of-bounds calls must return a sentinel larger than any real path — not 0, or the recursion will happily walk off the grid.
Solution
=LET(
tbl, A1:H8,
ros, ROWS(tbl),
cols, COLUMNS(tbl),
big, MAX(tbl) + SUM(tbl) + 1,
fx, LAMBDA(me, ro, co,
IF(OR(ro>ros, co>cols),
big,
IF(AND(ro=ros, co=cols),
INDEX(tbl, ro, co),
INDEX(tbl, ro, co) + MIN(me(me, ro+1, co), me(me, ro, co+1))
)
)
),
fx(fx, 1, 1)
)