Same as Unique Paths, but the grid has obstacles. obstacleGrid[r][c]=1 means blocked. Return the number of unique paths avoiding obstacles. Robot moves only right or down.
[[0,0,0],[0,1,0],[0,0,0]]2[[0,1],[0,0]]1Obstacle cells cannot be reached, so dp=0. Open cells inherit top+left. A 0 propagates through adjacent cells that depend on it, correctly blocking all paths through the obstacle.
Exploring every path and checking for obstacles repeats the same work many times. The same recurrence as before still works.
Use the same rule: paths into a cell equal paths from above plus paths from the left. But an obstacle cell gets 0, because no path can pass through it. This also handles obstacles in the first row or column: everything after them becomes 0. If the start itself is an obstacle, the answer is 0.
An obstacle simply forces the count to 0. In the rolling array, dp of c already holds the value from above.
Run the animation above on your own input, or watch the 1:52 video explanation of Unique Paths II with a dry run and Java code.