Count Paths with Obstacles
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
| A | B | C | D | E | F | G | |
|---|---|---|---|---|---|---|---|
| 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
| 3 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
| 4 | 0 | 1 | 0 | 0 | 0 | 1 | 0 |
| 5 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
| 6 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
| 7 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
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)
)