Dynamic Programming21 sections · 916 units
Open in Course

Grid 1 - Handling Walls

Blocked cells

What if cell (i,j)(i,j) is a wall (#)? You can't stand on a wall, so no paths can reach it. Set dp[i][j]=0dp[i][j] = 0 for any wall cell. Walls block paths completely.

When computing dp[i][j]dp[i][j] for a passable cell, treat each blocked neighbor as contributing 00 paths. Since 0+x=x0 + x = x, the formula dp[i][j]=dp[i1][j]+dp[i][j1]dp[i][j] = dp[i-1][j] + dp[i][j-1] works unchanged. Initialize all wall cells to 00 before running the DP.