Skip to content
BytePatterns

Grid Paths With Blocks

MediumDynamic Programming#grid-dp#bottom-up-dp~30m

Problem

A robot starts in the top-left cell of a grid and wants to reach the bottom-right cell, moving only right or down. Cells marked 1 are blocked and cannot be entered, while cells marked 0 are free. Count the distinct paths the robot can take, which is 0 when the start, the finish, or every route between them is blocked.

Examples

Input:  [[0, 0, 0],
         [0, 1, 0],
         [0, 0, 0]]
Output: 2
Why:    the blocked centre leaves one route along each edge
Input:  [[0, 1],
         [1, 0]]
Output: 0
Why:    both routes out of the start are blocked
Input:  [[0]]
Output: 1
Why:    edge case, the robot already stands on the finish

Hints

0 / 3

Stuck on the idea rather than the code? DP on Grids covers it.