Count Paths

Count Paths

In a grid of open cells (O) and walls (X), count the paths from the top-left to the bottom-right cell, moving only right or down and never onto a wall. If the start or end is a wall, the answer is 0.

countPaths(grid)

Input format: rows separated by |, each a string of O/X.

Examples

OOO|OOO|OOO   // → 6
OXO|OOO|OOO   // → 3
OO|XO         // → 1

Walkthrough

Breaking the grid into a choice at each cell

At any open cell, a path to the corner must go through the cell to the right or the cell below — the only two legal moves. So paths from here equal paths from the right plus paths from below.

Base cases

Standing on the destination counts as one path. A move off the grid or onto a wall contributes zero.

Why the naive recursion wastes work

Many different routes pass through the same cell — right-then-down and down-then-right can both reach the same intermediate cell. The naive recursion recomputes "paths from here to the end" independently for every route that arrives there, even though that count depends only on position, not history. Overlapping calls grow exponentially with grid size.

The subproblem: a position, not a path

The path count from a cell depends only on its (row, column) — nothing about how you got there. Cache keyed by (row, column). Check before recursing; if cached, return it. Otherwise sum the right and down recursive results, store, return.

Complexity payoff

Only rows × cols distinct cells exist, each computed once at constant cost given its neighbors' cached values — O(rows × cols) instead of exponential.

main.py
Console
Press Run to execute your code, or Submit to test and complete this problem.