← All challenges

Count Paths with Obstacles

Grid / Path ★★☆☆

Count distinct top-left to bottom-right paths through a 7×7 warehouse grid with walls (right or down moves only).

A1:G7 is a 7×7 warehouse layout. 0 is open floor, 1 is a shelf you can’t walk through. A picker enters at A1 (top-left) and must reach G7 (bottom-right), moving only right or down at each step.

Output the number of distinct routes through the open cells.

Without walls this would be C(12, 6) = 924 routes. The walls slash that dramatically — the formula has to enumerate and filter, which is why the recursive + over branches works cleanly here.

Input

Range A1:G7

ABCDEFG
1 0000000
2 0010000
3 0000100
4 0100010
5 0001000
6 0000010
7 0000000
Hint

Branching recursion. Three base cases: out-of-bounds returns 0, hitting a wall returns 0, reaching the goal returns 1. Otherwise recurse down + right and SUM the results.

Solution
=LET(
  tbl, A1:G7,
  ros, ROWS(tbl),
  cols, COLUMNS(tbl),
  fx, LAMBDA(me, ro, co,
    IF(OR(ro>ros, co>cols), 0,
    IF(INDEX(tbl, ro, co)=1, 0,
    IF(AND(ro=ros, co=cols), 1,
      me(me, ro+1, co) + me(me, ro, co+1)
    )))
  ),
  fx(fx, 1, 1)
)