Problem 529429 · hard · Phase 05 Advanced Algorithms & Graphs

Pictures Behind the Run Clues

backtracking · memoisation · puzzle solving · bitmasks

A puzzle book prints a grid of R = len(row_clues) rows and C = len(col_clues) columns with every cell blank. Each cell is to be shaded or left white. The clue of a row lists, from left to right, the lengths of its blocks of consecutive shaded cells; blocks are separated by at least one white cell. Column clues work the same way from top to bottom. An empty clue [] means that line has no shaded cell.

Return the number of shadings that match every row clue and every column clue.

Examples

Input:  row_clues = [[1], [1]], col_clues = [[1], [1]]
Output: 2
Explanation: shade the main diagonal or the other diagonal.

Input:  row_clues = [[3], [1, 1], [3]], col_clues = [[3], [1, 1], [3]]
Output: 1
Explanation: a square ring with a white centre.

Input:  row_clues = [[1], []], col_clues = [[1], [1]]
Output: 0

Constraints

  • 1 <= R, C <= 15
  • every clue is a list of positive integers that fits in its line
  • the answer can be large (Python integers do not overflow)

Goals

  • Fill a grid row by row while tracking how far each column's clue has progressed
  • Memoise on the row index and the column progress so equal partial grids are counted once
Starting Python…