Poj 2411 Mondriaan's Dream (State compression DP)

Source: Internet
Author: User

Click Open Link

Question:

There are two kinds of dominoes: 1*2 and 2*1. How many paving methods can be used in a rectangle filled with M * n?

Ideas:

In binary format, two consecutive 00 indicates that the two grids are horizontal, and 1 indicates that the grids are vertical (the upper part or the lower part of the bone card ).

First, process and obtain all the statuses that meet the condition. If there is 0, two zeros must appear consecutively.

For the state J of row I, where J is 1, then the same place of the I-1 line must also be 1 or 0. Line I and I-1 are both 1 place, they constitute a vertical card, then these places of I-2 line, either 0 or 1, so we need to change 1 to 0 in recursion, so that the place where the I-2 row can be 1 or 0.

F [I] [J] indicates the status of J in line I.

 

# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # include <vector> using namespace STD; typedef long int64; const int INF = 0x3f3f3f; const int max_state = (1 <11) + 10; const int maxn = 12; int n, m; int sta [max_state], idx; int64 f [maxn] [max_state]; int maxstate; bool OK [max_state]; bool check (INT Sta) {int I = 0; while (I <m) {If (STA> I) & 1) ++ I; else {if (I = S-1 | (S Ta> (I + 1) & 1 )! = 0) return false; I + = 2;} return true;} void Init () {memset (OK, 0, sizeof (OK )); maxstate = (1 <m)-1; idx = 0; For (INT I = 0; I <= maxstate; ++ I) {If (check (I )) {sta [idx ++] = I; OK [I] = true ;}}// int64 DFS (INT cur, int st) in the memory search) {If (F [cur] [st]! =-1) return f [cur] [st]; If (cur = 0) Return OK [st]; int64 ret = 0; For (INT I = 0; I <idx; ++ I) {If (STA [I] & St) = sT) {RET + = DFS (cur-1, st ^ sta [I]) ;}} return f [cur] [st] = ret;} // recursive dpint64 dp () {If (n = 1) return OK [0]; memset (F, 0, sizeof (f); int ans = 0; For (INT I = 0; I <n; ++ I) {if (I = 0) {for (Int J = 0; j <idx; ++ J) f [I] [sta [J] = 1 ;} else {for (int s = 0; S <= maxstate; ++ s) {for (int K = 0; k <idx; ++ K) {If ((STA [k] & S) = s) f [I] [s] + = f [I-1] [sta [k] ^ s] ;}}} for (INT I = 0; I <idx; ++ I) ans + = f [n-1] [sta [I]; return f [n-1] [0];} int main () {While (~ Scanf ("% d", & N, & M) & N + M) {Init (); memset (F,-1, sizeof (f )); // cout <DFS (n-1, 0) <Endl; cout <dp () <Endl ;}return 0 ;}

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.