Zoj 1100 Paving Brick State compression

Source: Internet
Author: User
Tags printf

1, the ST is stored in each row of all possible, 0 is empty, 1 is occupied, st[i][0] in the possible state of the deposit Line (From,st[i][1] in the next line of the state that matches with to;

2, Dfs from is a row of the first n lattice state, to is the state of the match;

3, because only look at the possible state of each row, so DFS in the n==w when the possibility has been taken, exit recursion;

4, for the row of the nth column, take three kinds of the way of placing rectangles

DFS (n+2, (from<<2) +3, (to<<2) +3); Horizontal, this layer and the next layer match more than two bits 1 (binary 11 is 3)
DFS (n+1, (from<<1) +1,to<<1);//vertical, this layer one more 1, matching the lower level this position is 0
DFS (n+1,from<<1, (to<<1) +1);//Do not put, this layer more than a 0, matching the lower layer of more than 1

5. Dp[i][j] The number of methods in which the state of deposit I is placed at J, the boundary is to be fully filled, dp[0][(1<<w) -1]=1, the last requirement is dp[h][(1<<W)-1];

6. DP process is

for (i=1;i<=h;i++)
{
for (j=0;j<cnt;j++)
{
Dp[i][st[j][1]]+=dp[i-1][st[j][0]];
}

}

#include <stdio.h> #include <string.h> int st[3000][2]; int dp[11][3000];
2^11=2048 Maximum number of States int cnt,h,w;
    void Dfs (int n,int from,int to)//enumerates all rows of possible and matching underlying {if (n>w) return;
        if (n==w)//This line is just finished, get a row of combinations {st[cnt][0]=from;
        St[cnt][1]=to;
        cnt++;
    Return } dfs (n+2, (from<<2) +3, (to<<2) +3); Horizontal, this layer and the next layer match more than two bit 1 Dfs (n+1, (from<<1) +1,to<<1);//vertical, this layer one more 1, matching the lower level this position is 0 Dfs (n+1,from<<1, (to
    <<1) +1);//Do not put, this layer more than one 0, matching the underlying more than 1} int main () {int i,j;
            while (scanf ("%d%d", &h,&w) &&h) {if ((w*h)%2) {printf ("0\n");
        Continue
        } memset (Dp,0,sizeof (DP));
        memset (st,0,sizeof (ST)); cnt=0;
        Record the total number of possible states in each row DFS (0,0,0);
                dp[0][(1<<w) -1]=1;//boundary for (i=1;i<=h;i++) {for (j=0;j<cnt;j++) {
    Dp[i][st[j][1]]+=dp[i-1][st[j][0]];        }} printf ("%d\n", dp[h][(1&LT;&LT;W)-1]);
} 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.