Tiling problem (state compression DP)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.