状压DP

Mondriaan's Dream
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 9938 Accepted: 5750

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

Ulm Local 2000

用0表示竖着放的木块的上半部分,单独的1表示竖着的木块的下半部分,两个连续的1表示横着放的木块.
最后一行铺满的状态就是全部为1

上下行的兼容性:
 1:下面为0上面一定不能为0
 2:下面为1.....如果上面为0,那么这是一个单独的1
                    如果上面为1,那么这应该是一个连续的1,检查下一位是否为1,及下一位的上面是否为1

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

long long int dp[14][1<<14];
int r,c;

bool isfirst(int x)
{
    int ans=0;
    for(int i=0;i<c;i++)
        if((x>>i)&1) ans++;
    if(ans%2==1) return false;
    for(int i=0;i<c;i++)
    {
        if((x>>i)&1)
        {
            if((x>>(i+1))&1)
                i++;
            else
                return false;
        }
    }
    return true;
}

void getfirstline()
{
    for(int i=0;i<(1<<c);i++)
    {
        if(isfirst(i))
        {
            dp[1]=1;
/*
            for(int j=0;j<c;j++)
            {
                printf("%d ",(i>>j)&1);
            }
            putchar(10);
*/
        }
    }
}

bool isCool(int xia,int shang)
{
    for(int i=0;i<c;i++)
    {
        if(((xia>>i)&1)==0)
        {
            if(((shang>>i)&1)==0) return false;
        }
        else
        {
            if(((shang>>i)&1)==0) continue;
            else
            {
                if(i+1>=c) return false;
                if(((xia>>(i+1))&1)==1)
                {
                    if(((shang>>(i+1))&1)==1)
                        i++;
                    else
                        return false;
                }
                else
                    return false;
            }
        }
    }
    return true;
}
int main()
{
    while(scanf("%d%d",&r,&c)!=EOF&&r&&c)
    {
        memset(dp,0,sizeof(dp));
        if(r<c) swap(r,c);
        getfirstline();
        for(int i=2;i<=r;i++)
        {
            for(int j=0;j<(1<<c);j++)
            {
                for(int k=0;k<(1<<c);k++)
                {
                    if(isCool(j,k))
                    {
                        dp[j]+=dp[i-1][k];
                    }
                }
            }
        }
        printf("%I64d\n",dp[(1<<c)-1]);
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

POJ 2411 Mondriaan&#39;s Dream的更多相关文章

  1. poj 2411 Mondriaan&#39;s Dream 【dp】

    题目:id=2411" target="_blank">poj 2411 Mondriaan's Dream 题意:给出一个n*m的矩阵,让你用1*2的矩阵铺满,然 ...

  2. POJ 2411 Mondriaan&#39;s Dream (dp + 减少国家)

    链接:http://poj.org/problem?id=2411 题意:题目描写叙述:用1*2 的矩形通过组合拼成大矩形.求拼成指定的大矩形有几种拼法. 參考博客:http://blog.csdn. ...

  3. 状压DP POJ 2411 Mondriaan'sDream

    题目传送门 /* 题意:一个h*w的矩阵(1<=h,w<=11),只能放1*2的模块,问完全覆盖的不同放发有多少种? 状态压缩DP第一道:dp[i][j] 代表第i行的j状态下的种数(状态 ...

  4. POJ 2411 Mondriaan's Dream 插头dp

    题目链接: http://poj.org/problem?id=2411 Mondriaan's Dream Time Limit: 3000MSMemory Limit: 65536K 问题描述 S ...

  5. Poj 2411 Mondriaan's Dream(压缩矩阵DP)

    一.Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, ...

  6. POJ 2411 Mondriaan's Dream -- 状压DP

    题目:Mondriaan's Dream 链接:http://poj.org/problem?id=2411 题意:用 1*2 的瓷砖去填 n*m 的地板,问有多少种填法. 思路: 很久很久以前便做过 ...

  7. poj 2411 Mondriaan's Dream(状态压缩dP)

    题目:http://poj.org/problem?id=2411 Input The input contains several test cases. Each test case is mad ...

  8. poj 2411 Mondriaan's Dream(状态压缩dp)

    Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, af ...

  9. poj 2411 Mondriaan's Dream 轮廓线dp

    题目链接: http://poj.org/problem?id=2411 题目意思: 给一个n*m的矩形区域,将1*2和2*1的小矩形填满方格,问一共有多少种填法. 解题思路: 用轮廓线可以过. 对每 ...

随机推荐

  1. POJ 2457 Part Acquisition

    第一反应是BFS,比较直观,但是输出路径写的不是很熟练,此外,习惯不好,“==”写成了“=”,所以常量一定放前面! #include <cstdio> #include <queue ...

  2. GNU CMAKE 笔记

    最近在调试OJ, 忙了4天多, 最后的问题是judge模块不能正常工作. judge 模块就是两个C++源文件, 它的工作是 从数据库获取用户提交的源码 测评 将测评结果写到数据库 测评部分是与数据库 ...

  3. [Android]加密技术

    对称加密无论是加密还是解密都使用同一个key,而非对称加密需要两个key(public key和private key).使用public key对数据进行加密,必须使用private key对数据进 ...

  4. Django TemplateSyntaxError Could not parse the remainder: '()'

    返回的数据是列表集合,如 n [5]: a = set() In [6]: a.add((1, 3)) In [7]: a Out[7]: {(1, 3)} 在模板中使用方式如下: {% for ar ...

  5. elk系列3之通过json格式采集Nginx日志

    preface 公司采用的LNMP平台,跑着挺多nginx,所以可以利用elk好好分析nginx的日志.下面就聊聊它吧. 下面的所有操作都在linux-node2上操作 安装Nginx nginx是开 ...

  6. gcc编译与gdb调试简要步骤

    http://blog.chinaunix.net/uid-24103300-id-108248.html 一.Linux程序gcc编译步骤: Gcc编译过程主要的4个阶段: l 预处理阶段,完成宏定 ...

  7. 什么是xmlschema

    XML Schema定义(XML Schema Definition,XSD)是一套W3C标准,用于基于XML的称为XML Schema的类型系统.用于定义的语言是一种称为XML模式定义语言(XML ...

  8. HashMap与ArrayList互相嵌套的代码实现

    HashMap嵌套ArrayList的代码实现 结果要求为 三国演义            吕布            周瑜笑傲江湖           令狐冲            林平之神雕侠侣  ...

  9. C#------如何获取本机IP地址

    转载: http://www.narkii.com/club/thread-396944-1.html 代码: private string GetIpAddress() { string hostN ...

  10. 20145212 《Java程序设计》第5周学习总结

    20145212 <Java程序设计>第5周学习总结 教材学习内容总结 第八章 1.Java中所有错误都会被打包为对象,通过try和catch语法可以对代表错误的对象做处理. try{ . ...