Given a pattern of squares on a grid, determine if it is possible to create that pattern with non-overlapping dominoes. In case you are not familiar, a domino is a rectangular shape created by joining exactly two squares at their edges.
Examples
For the pattern on the left, O
represents an occupied cell on the grid and .
represents an empty cell. For the pattern to the right of the first |
, numbers and letters will be used to mark individual dominoes in a possible solution
Possible
O | 1 | This is the trivial case:
O | 1 | a single domino laid vertically
. O . . | . 2 . . | This can be created with three
O O O O | 1 2 3 3 | dominoes in a simple pattern
O . . . | 1 . . . |
O O O O | 1 1 2 2 | A simple rectangular grid with
O O O O | 3 3 4 4 | even width is easily tiled with
O O O O | 5 5 6 6 | horizontally-laid dominoes
O O O | 1 1 2 | Four dominoes laid radially
O . O | 3 . 2 |
O O O | 3 4 4 |
. O O . | . 1 1 . | Dominoes do not need to touch
O . O . | 2 . 3 . | and the grid may contain empty
O . O . | 2 . 3 . | cells along an edge
. O . . O O O O O . | . K . . R R S S N . | A 10x10 test case and
O O O . . O . . O . | U K J . . 5 . . N . | one of its solutions
O . O . . O . O O O | U . J . . 5 . C C Q |
O O O O O O . O . O | T B B 4 1 1 . 7 . Q |
O . . O O O . O . . | T . . 4 6 6 . 7 . . |
. . O O O . O O O . | . . 2 3 3 . 8 8 D . |
O O O . . O . . O . | I I 2 . . 9 . . D . |
. . O O O O . O O O | . . G O O 9 . E E L |
. . O . . O O O . O | . . G . . A F F . L |
O O . O O O . . O O | M M . H H A . . P P |
Not Possible
O | You need at least two occupied cells to fit a domino
O . | Dominoes are squares joined by edges, not corners
. O |
O | It is always impossible to create a pattern with an odd
O | number of squares with dominoes
O |
O O . O | No matter how you lay the first few dominoes,
. O O O | at least two squares are always separated
. O . O |
O O O . | This is a slightly more complicated version of the above
O . O O |
O O O . |
. O O O |
. O . . . . | A small test case that cannot be decided with
O O O O O O | a chessboard painting algorithm
. . . . O . |
. O O O O O . O O O | A 10x10 example test case
O O O . . O . . . O |
. . O . O . O O O . | This pattern is almost possible
. O . . O . . . O O | except that the bottom-left corner
. O O O O O O O . O | contains an arrangement which is
. . . . O . . O O O | impossible to make with dominoes
O O O O O . O . . . |
O . O . . . O . . O |
. O O O . O O . O O |
. . . O O . O O O . |
. O O O O O O O O O | A pathological case for a chessboard
O O O O O O O O O . | painting algorithm.
O O O O O O O O O O |
O O O O O O O O O O | This is also a pathological case for
O O O O O O O O O O | a backtracking algorithm.
O O O O O O O O O O |
O O O O O O O O O O |
O O O O O O O O O O |
O O O O O O O O O O |
O O O O O O O O O O |
Rules and Scoring
- This is Code Golf, so shortest code wins
- Use any convenient I/O method.
- Valid input formats for the grid include, but are not limited to:
- Array of arrays
- Height, width, array
- Array of integers representing each row (or column) in binary
- A string representation similar to the examples above
- A PNG image
- You may assume input grids are rectangular (not jagged)
- Your solution should return answers within a reasonable amount of time (it should not time out on Try It Online, for example) for inputs up to 10x10 and be able to theoretically work for a grid of any size if given enough time and space.
. O . . . . / O O O O O O / . . . . O .
-> false \$\endgroup\$O
‘s are placed on black vs white cells. And the layout cannot be made by dominos if these two numbers are not equal. Although equality of these two numbers does not mean it must be possible layout. \$\endgroup\$