问题
I want to find the number of possible different combinations for a 4 x N area (4 units width and N units height, N ≥ 1) of domino bricks using dynamic programming .
Domino bricks have a size of 2x1 e.g.
==
for a horizontal and
|
|
for a vertical brick.
Now,
Example 4x1 (two domino bricks beneath each other)
====
Examples for 4x2 brick configurations (5 in total)
1)
||||
||||
2) (Turn two bricks on the right)
||==
||==
3)
|==|
|==|
4)
====
====
5)
==||
==||
Number of unique combinations known so far
4x1 : 1 possibility
4x2 : 5 possibilites
4x3 : 11 possibilites
4x4 : 36 possibilites
回答1:
Solve a more general problem. Find the number of ways to tile a 4×N grid where some of the positions in the top row may be occupied. Associate each position with a power of 2, leftmost corresponds to 1, second 2, third 4, rightmost 8. Let T(N,k) be the number of tilings of a 4×N grid where the positions corresponding to k in the top row are already occupied. k == 0 means no position occupied, k == 6 means the two middle positions are occupied (6 = 2 + 4) etc.
Then find the transitions, when filling the remainder of the top row, which patterns in the next row are reachable in how many ways?
For example, if the middle two positions are occupied, the only way to fill the remainder of the top row is to place a domino vertically in the leftmost and the rightmost position, leading to
|xx|
| |
and a configuration in which the two outermost positions in the next row are occupied, that corresponds to 1+8 = 9, so T(N,6) = T(N-1,9). And for k == 9, the situation we start from looks
| |
and we have two possibilities,
|==| ||||
||
we can either fill the gap by placing one domino horizontally, leaving the next row completely free, or place two dominoes vertically, occupying the two middle positions of the next row, so
T(N,9) = T(N-1,0) + T(N-1,6)
Use these transitions to build a table of the T(n,k).
The value you want to find is T(N,0).
回答2:
F[n] = number of ways to tile a 4-by-n grid
G[n] = number of ways to tile a 4-by-n grid with top-right and bottom-right
squares uncovered
H[n] = number of ways to tile a 4-by-n grid with bottom-right 2
squares uncovered
= number of ways to tile a 4-by-n grid with top-right 2
squares uncovered
if n >= 2, the right end of any tiling can be
two vertical dominoes (F[n-1] ways)
horz, horz vert (H[n-1] ways)
horz, vert, horz (G[n-1] ways)
vert, horz, horz (H[n-1] ways)
4 horizontal dominoes (F[n-2] ways)
F[n] = F[n-1] + G[n-1] + 2*H[n-1] + F[n-2];
For G: the right end can be a vertical domino (F[n-1] ways)
or two horizontal dominoes => top & bottom are horz = G[n-2]
G[n] = F[n-1] + G[n-2];
For H: the right end can be a vertical domino (F[n-1] ways)
or two horizontal dominoes (H[n-1] ways)
H[n] = F[n-1] + H[n-1];
F[0] = 1, F[1] = 1, G[0] = 0, G[1] = 1, H[0] = 0, H[1] = 1
Hope it helps !!
来源:https://stackoverflow.com/questions/10727920/number-of-combinations-for-4xn-domino-bricks