Skip to contentKEEP THE CRAFTTHE SANCTUARY

Memoize Grid Paths

Count down-and-right paths through a grid with blocked cells, caching each cell's answer.

The problem

Implement count_grid_paths(rows, cols, blocked). You start at the top-left cell (0, 0) of a rows x cols grid and may only move down or right. Return the number of different paths that reach the bottom-right cell (rows - 1, cols - 1). blocked is a list of [row, col] cells you may not step on. If the start or the destination is blocked, return 0. For example, a 3 x 3 grid has 6 paths, and only 2 of them avoid a blocked center cell [1, 1].

Starter code

def count_grid_paths(rows, cols, blocked):
    pass

Public examples

  1. rows = 3, cols = 3, blocked = []gives6
  2. rows = 3, cols = 3, blocked = [[1, 1]]gives2
  3. rows = 2, cols = 2, blocked = [[1, 1]]gives0

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.