1814. Holey Domino Tilings

A tiler has a rectangular floor described by grid. A cell marked '.' must be covered and a cell marked '#' is a hole that must stay uncovered. The tiler has an unlimited supply of 1 x 2 dominoes, each of which can be laid horizontally or vertically over exactly two edge-adjacent '.' cells. Dominoes may not overlap, may not cover a hole and may not stick out of the floor.

Return the number of different ways to cover every '.' cell exactly once. Two ways are different if some domino position differs. If there is nothing to cover the answer is 1 (the empty tiling), and if the cells cannot be tiled it is 0. Plain backtracking explodes on large open floors, so process the cells one at a time and keep, as the state, a bitmask of which of the next cols cells are already covered by a domino placed earlier. Time is O(rows * cols * 2^cols) and space O(2^cols).

Example 1

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

A 2 x 2 block is covered either by two horizontal dominoes or by two vertical ones, giving 2 ways.

Example 2

Input:
grid = [[".",".","."],[".",".","."]]
Output:
3
Explanation:

A 2 x 3 block can be tiled in 3 different ways.

Example 3

Input:
grid = [[".","#","."],[".",".","."]]
Output:
0
Explanation:

The cell at the top-middle is blocked and the other five cells must be covered, which cannot be paired up, so the count is 0.

Constraints

1 ≤ rows, cols ≤ 10

grid[i][j] is '.' or '#'

The answer is less than 253

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 6,000 msC++ 1,500 msJava 3,000 msJavaScript 3,000 msTypeScript 3,000 ms

Expected complexity

Time
O(rows * cols * 2^cols)
Space
O(2^cols)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…