Test instructions
Given a n*m lattice, each lattice is dyed black or white. Now to cover these squares with 1 * 2 bricks, it is required that the blocks and blocks do not overlap each other, and that all white squares are covered, but no black squares are covered. Find out how many kinds of coverage methods, the output scheme number to m after the result of the remainder.
Input:
N= 3
M= 4
The color of each lattice is as follows (. = white, x for Black)
...
. x.
...
Output:
2
Analysis:
Since the black lattice cannot be overwritten, the corresponding position in the used is always false. For white lattices, if the bricks are to be placed at the (i,j) position now, they will always be placed from the top of the grid, so the corresponding (i ', J ') < (I.J) (i ', J ') is always used[i '][j ']=true.
Furthermore, since the size of the bricks is 1*2, for each column J ' in Satisfies (i ', J ') >= (I.J) All I ', except the smallest I ', satisfies used[i '][j ']=false. Therefore, it is uncertain that only each joins has not queried the topmost one of the lattice, a total of M. This allows the M-lattice to be memorized by the state compression code. The following procedure is obtained by compressing the DP in the previous state.
int dp[1 << MAXN]; DP Array (scrolling array recycling) void solve () {int *crt = dp[0], *next = dp[1]; Crt[0] = 1; for (int i = n-1; I >= 0, i--) {for (int j = m-1, j >= 0; j--) {for (int used = 0; used < 1 << m; used++) {if (used >> J & 1) | | color[i][j]) {//Do not need to place bricks in (I, J) Next[used] = crt[used & ~ (1 << j)]; } else{//Try 2 different methods of putting int res = 0; Cross-put if (j + 1 < m &&!) Used >> (j + 1) & 1) &&!color[i][j + 1]) {res + = crt[used | 1 <<< (j + 1)]; }//Vertical put if (i + 1 < n &&!color[i + 1][j]) {res + = crt[used | 1 << j]; } next[used] = res% M; }} swap (CRT, Next); }} printf ("%d\n", Crt[0]);}
Tiling problem (state compression DP)