POJ-2411 Mondriaan & amp; #39; s Dream (State compression)

Source: Internet
Author: User

POJ-2411 Mondriaan & #39; s Dream (State compression)

Question: How many paving methods are required to fill the n * m mesh with 1*2 or 2*1 bricks?

Solution: at the beginning, we used 3 tabulation to display the status of each row. 0 indicates part of the bricks of 2*1, and 1 indicates the upper part of the bricks of 1*2, 2 represents the next part of the 1*2 brick, and then solve the problem like the poj-1185 artillery positions, the results found that the status is too much, Will TLE, had to give up
Refer to other people's code later. You can convert the code into a binary representation. 0 indicates that the position is not paved, and 1 indicates that the position has been paved.
Because 1*2 affects the existence of bricks in the two rows, it is necessary to determine whether the two states can be consistent. Only matching can be done.
Classification Discussion
1. If column j of row I is 1

A. if column j of the row of the I-1 is also 1, then the brick here is not likely to be 1*2 of this type of brick (affect the brick of the row 2 ), that is, if both of them are paved with 2*1 bricks, it is necessary to judge whether the adjacent one is also 1. If not, it indicates that the two States do not match B. if column j of the row I-1 is 0, the brick of column j of the row I is 1*2, the preceding section stores column j of the row of the I-1

2. If column j of row I is 0

A. if column j of the row of the I-1 is also 0, then there is a vacancy, does not meet B. if column j of the row of the I-1 is 1, it indicates that the row I + 1 needs the brick of 1*2 to fill

You can refer to the topic of the great god to write the link content here.
A code

#include
  
   #include
   
    #includeusing namespace std;#define maxn 15#define maxs (1 << 12)long long ans[maxn][maxn];long long dp[maxn][maxs];int h, m;bool ok(int s) {    for(int i = m - 1; i >= 0; i--) {        if(s & (1 << i)) {            if(i == 0)                return false;            if(!(s & (1 << (i - 1))))                return false;            i--;        }    }    return true;}bool judge(int s, int ss) {    for(int i = (m - 1); i >= 0; i--) {        if(!(ss & (1 << i)) && !(s & (1 << i)))             return false;        if((s & (1 << i))) {            if(ss & (1 << i)) {                if(i == 0)                    return false;                if(!(s & (1 << (i - 1))) || !(ss & (1 << (i - 1))))                    return false;                i--;            }        }    }    return true;}void solve() {    int t;    if(h < m) {        t = h;        h = m;        m = t;    }    memset(dp, 0, sizeof(dp));    for(int i = 0; i < (1 << m); i++)        if(ok(i))            dp[1][i] = 1;       for(int r = 2; r <= h; r++)        for(int s = 0; s < (1 << m); s++)            for(int ss = 0; ss < (1 << m); ss++)                if(judge(s, ss))                    dp[r][s] += dp[r-1][ss];    printf(%lld, ans[h][m] = ans[m][h] = dp[h][(1 << m) - 1]);}int main() {    memset(ans, -1, sizeof(ans));    while(scanf(%d%d, &h, &m) != EOF && h + m) {        if(ans[h][m] != -1) {            printf(%lld, ans[h][m]);            continue;        }        if((h * m) % 2 == 1) {             ans[h][m] = ans[m][h] = 0;            printf(0);            continue;        }        solve();    }    return 0;}
   
  

Code for triplicate timeout

#include
  
   #include
   
    #include#include
    
     using namespace std;#define maxn 270000int state[maxn];int dp[12][maxn];int w, h, cnt;int mod[13];void init() {    memset(dp, 0, sizeof(dp));    cnt = 0;    bool flag1, flag2;    for(int i = 0; i < mod[w]; i++) {         flag1 = flag2 = false;        for(int j = w - 1; j >= 0; j--) {            if((i % mod[j + 1] - i % mod[j]) / mod[j] == 0) {                if(j == 0) {                    flag1 = true;                    break;                }else {                    if((i % mod[j] - i % mod[j - 1]) / mod[j - 1] != 0) {                        flag1 = true;                        break;                    }                }                j--;            }        }        if(!flag1) {            for(int j = w - 1; j >= 0; j--) {                if((i % mod[j + 1] - i % mod[j]) / mod[j] == 2) {                    flag2 = true;                    break;                }            }        }        if(!flag1)            state[cnt++] = i;         if(!flag1 && !flag2)            dp[0][i] = 1;    }}int solve() {    for(int r = 1; r < h; r++)        for(int i = 0; i < cnt; i++)            for(int j = 0; j < cnt; j++) {                bool flag = false;                for(int k = w - 1; k >= 0; k--) {                    if(((state[j] % mod[k + 1] - state[j] % mod[k] ) / mod[k] == 1) && ( (state[i] % mod[k + 1] - state[i] % mod[k] ) / mod[k] != 2)) {                        flag = true;                        break;                    }                    if(((state[i] % mod[k + 1] - state[i] % mod[k] ) / mod[k] == 2) && ( (state[j] % mod[k + 1] - state[j] % mod[k] ) / mod[k] != 1)) {                        flag = true;                        break;                    }                }                if(!flag) {                    dp[r][state[i]] += dp[r-1][state[j]];                }            }    int ans = 0;    for(int i = 0; i < cnt; i++) {        bool flag = false;        for(int j = w - 1; j >= 0; j--)            if((state[i] % mod[j + 1] - state[i] % mod[j]) / mod[j] == 1) {                flag = true;                break;            }        if(!flag) {            ans += dp[h - 1][state[i]];        }    }    return ans;}void begin() {    mod[0] = 1;    for(int i = 1; i < 13; i++)        mod[i] = mod[i - 1] * 3;}int main() {    begin();    while(scanf(%d%d, &h, &w) != EOF && h + w) {        if((h * w) % 2 == 1)            printf(0);        else {            init();            printf(%d, solve());        }    }    return 0;}
    
   
  

 

Related Article

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.