← All challenges

Grid Min Path Sum

Grid / Path ★★☆☆

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

ABCDEFGH
1 1971812152177
2 2081016711153
3 97631518127
4 1611813147314
5 1510742011913
6 1381431314115
7 718412112121
8 9195214693
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)
)