Problem 483497 · medium · Phase 04 Non-Linear Data Structures

Courier Routes Around Roadworks

recursion · memoisation · grid paths

A courier crosses a city grid given as a list of equal-length strings, where '.' is an open block and '#' is closed for roadworks. Starting at the top-left cell, the courier may only move right or down, and must reach the bottom-right cell. Write route_count(grid) returning the number of distinct routes. If the start or the destination is closed the answer is 0.

Examples

Input:  grid = [".#.",
                "...",
                ".#."]
Output: 1
Explanation: the only route goes down, down, right, right... no: down, right, right, down.

Input:  grid = ["..",
                ".."]
Output: 2

Constraints

  • 1 <= rows, cols <= 60
  • A memoised recursion needs at most rows + cols stack levels.

Goals

  • Express a grid path count in terms of two neighbouring cells
  • Treat blocked cells and out-of-range cells as contributing zero
  • Memoise on the cell coordinates
Starting Python…