Dynamic Programming21 sections · 916 units
Open in Course

Out of Boundary - Walkthrough

Counting paths with probability

Count paths that exit an m×nm \times n grid in exactly kk moves. Each move goes up/down/left/right. dp[step][r][c]dp[step][r][c] = number of paths at (r,c)(r, c) after stepstep moves. Transitions: each cell receives from 4 neighbors.

When a move goes out of bounds, it counts as an exit. Track exits at each step. Total exits = sum of paths that exit at each step from 1 to kk. Take modulo as counts get large. Use modular arithmetic for counting paths. The mod operation distributes over addition correctly.