1. The playground
Click or drag on the grid to add or remove obstacles. S is the start, E is the end, and a blocked cell contributes zero paths. Press Animate fill to watch the table compute row by row; the number in E is the answer.
2. What to notice
- Each cell depends only on the cell above and the cell to the left. Once a cell is computed, its value never changes.
- The first row and first column are all 1 when there are no obstacles: there is only one straight-line route to each.
- An obstacle can cut the count dramatically. A blocked start or end makes the answer zero.
- The table is the memoization. Without it, the same subgrid would be counted again and again.
3. The recurrence
The recurrence is the whole algorithm. The base case is the start cell; a blocked cell is zero; every other cell is the sum of its two predecessors.
Grid paths
Time is O(rows × cols). The full table uses O(rows × cols) space; if you only need the count, the previous row is enough.