Dynamic Programming21 sections · 916 units
Open in Course

Grid 1 - Transition

Coming from above or left

To reach cell (i,j)(i,j), you must have come from either:

1.1. Cell (i1,j)(i-1, j) - you moved down

2.2. Cell (i,j1)(i, j-1) - you moved right

So the transition is: dp[i][j]=dp[i1][j]+dp[i][j1]dp[i][j] = dp[i-1][j] + dp[i][j-1] The paths from above plus the paths from the left. If either neighbor is out of bounds, treat it as 0. The transition is the core of any DP solution. For a blocked cell, setting dp[i][j]=0dp[i][j] = 0 ensures the formula dp[i][j]=dp[i1][j]+dp[i][j1]dp[i][j] = dp[i-1][j] + dp[i][j-1] handles walls automatically.