1821. Piece Box Packing
A toy box floor is described by board, a list of equal-length strings: '.' is a free cell that must be filled and '#' is blocked. You are also given pieces; pieces[i] lists the [row, col] cells of the i-th piece, a connected shape of one to six cells.
Return true if the pieces can be laid on the board so that every '.' cell is covered exactly once, every piece is used exactly once, no piece touches a '#' cell or leaves the board, and no two pieces overlap. Each piece may be rotated by any multiple of 90 degrees and may also be mirrored. Otherwise return false, including when the total area differs from the number of free cells. List every distinct orientation of each piece, then always cover the first uncovered cell in row-major order with an unused orientation whose first cell lands on it. Time is exponential in the worst case but small with this pruning; space is O(rows * cols).
Example 1
- Input:
- board = ["...","..."]pieces = [[[0,0],[1,0],[1,1]],[[0,0],[1,0],[1,1]]]
- Output:
- true
- Explanation:
Two L-shaped pieces fit together to fill the 2 x 3 board, one rotated half a turn.
Example 2
- Input:
- board = ["...","..."]pieces = [[[0,0],[0,1],[0,2]],[[0,0],[1,0],[1,1]]]
- Output:
- false
- Explanation:
The areas match (6 cells each), but a straight piece of three leaves an L-shaped hole that no other piece can fill.
Example 3
- Input:
- board = ["..#","#.."]pieces = [[[0,1],[0,2],[1,0],[1,1]]]
- Output:
- true
- Explanation:
The free cells form a zig-zag and the given piece is its mirror image, so it fits only when mirrored.
Constraints
1 ≤ rows, cols ≤ 8
board[i] consists of '.' and '#'
1 ≤ pieces.length ≤ 12; each piece has 1 to 6 distinct, 4-connected cells with row and col in 0..5
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 8,000 msC++ 2,000 msJava 4,000 msJavaScript 4,000 msTypeScript 4,000 ms
Expected complexity
- Time
- O(P * 8 * cells) per search step, exponential worst case
- Space
- O(rows * cols)