Unique Paths with Obstacles visualization

Count right/down paths from top-left to bottom-right avoiding walls.

Each cell can only be entered from above or from the left, so ways[r][c] = ways[r−1][c] + ways[r][c−1]. Walls have 0 ways.

Edge cases: if the start or the goal is a wall the answer is 0; a 1×1 open grid has exactly one (empty) path.

Complexity

Time: best O(R·C), average O(R·C), worst O(R·C). Space: O(R·C). Difficulty: Beginner.

Pseudocode

procedure uniquePaths(grid):
  if grid[0][0] is wall: return 0
  ways[0][0] ← 1
  for each cell (r, c) in row-major order:
    if wall: ways[r][c] ← 0
    else: ways[r][c] ← ways[r−1][c] + ways[r][c−1]
  return ways[R−1][C−1]

More dynamic programming algorithms