zoukankan      html  css  js  c++  java
  • POJ2411 Mondriaan's Dream(状态压缩)

    Mondriaan's Dream
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 15295   Accepted: 8820

    Description

    Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series' (where he had to use his toilet paper to draw on, for all of his paper was filled with squares and rectangles), he dreamt of filling a large rectangle with small rectangles of width 2 and height 1 in varying ways.

    Expert as he was in this material, he saw at a glance that he'll need a computer to calculate the number of ways to fill the large rectangle whose dimensions were integer values, as well. Help him, so that his dream won't turn into a nightmare!

    Input

    The input contains several test cases. Each test case is made up of two integer numbers: the height h and the width w of the large rectangle. Input is terminated by h=w=0. Otherwise, 1<=h,w<=11.

    Output

    For each test case, output the number of different ways the given rectangle can be filled with small rectangles of size 2 times 1. Assume the given large rectangle is oriented, i.e. count symmetrical tilings multiple times.

    Sample Input

    1 2
    1 3
    1 4
    2 2
    2 3
    2 4
    2 11
    4 11
    0 0
    

    Sample Output

    1
    0
    1
    2
    3
    5
    144
    51205
    

    Source

    /*
        已经给了时间看题解了,正式练习一道题三天之内不准再看题解!
    */
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #define N (1<<12)+5
    #define M 12
    /*
    用1表示当前小方格填满了,0表示没填满,初始化第一行的状态然后递推到最后一行
    */
    using namespace std;
    long long dp[M][N];//dp[i][j]表示第i行j种状态做多有多少种排列方式
    int n,m;
    bool check(int x)//判断是不是有连续个奇数个1;当有连续奇数个1的时候那么这一行横着放的肯定是不会填满的
    {
        int s=0;
        while(x)
        {
            if(x&1)s++;
            else
            {
                if(s&1)return false;
                s=0;
            }
            x>>=1;
        }
        if(s&1)return false;
        return true;
    }
    int main()
    {
        ///for(int i=0;i<30;i++)
        //    cout<<"i="<<i<<" "<<check(i)<<endl;
        //cout<<endl;
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        while(scanf("%d%d",&n,&m)!=EOF&&n&&m)
        {
            if((n*m)%2)//如果乘积是奇数的话是肯定铺不满的
            {
                printf("0
    ");
                continue;
            }
            if(n<m)//这里做一个剪枝,使m永远是小的那个,能减少不少循环的次数
            {
                int tmp=n;
                n=m;
                m=tmp;
            }
            int tol=(1<<m);
            memset(dp,0,sizeof dp);
            for(int i=0;i<tol;i++)//初始第一行的状态
                if(check(i))
                    dp[1][i]=1;
            for(int i=1;i<n;i++)//由i行推出i+1行
                for(int j=0;j<tol;j++)//枚举上一个状态
                    if(dp[i][j]!=0)//上一个状态为零没计算意义
                    {
                        for(int k=0;k<tol;k++)
                        {
                            if( (j|k)==tol-1 && check(j&k) )//这一步很精妙,(j|k)==tol-1保证了肯定能填满上一行,check(j&k)保证了这一行中横着放的小木块绝对能填满
                                dp[i+1][k]+=dp[i][j];
                        }    
                    }
            printf("%lld
    ",dp[n][tol-1]);
        }
        return 0;
    }
  • 相关阅读:
    android:descendantFocusability用法简析
    离开自己的安乐窝
    Android Material Design 中文版
    android Material
    jquery 效果网址分享
    Android判断TextView是否超出加省略号
    如何摆脱工具类
    android 框架
    gridview 横向滚动 一行显示
    自定义 spinner
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/5742648.html
Copyright © 2011-2022 走看看