Dynamic Programming21 sections · 916 units
Open in Course

Out of Boundary - State Definition

Position and moves

Define dp[r][c][k]dp[r][c][k] = number of paths starting at (r,c)(r, c) with exactly kk moves remaining that eventually leave the grid.

The difference from knight: we're counting paths, not computing probability. And leaving the grid is success, not failure. We need to sum over kk from 11 to maxMovemaxMove. A path can leave on move 1, move 2, ..., or move maxMovemaxMove. This dp definition has all the information needed to count paths from (r, c) with k moves.