状压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. 谈谈favicon和他带来的问题

    favicon.ico介绍 favicon.ico是个什么东西呢,也许见得太多都习以为常了(我就是这样,直到写这篇文章之前才知道),看看维基百科的解释: Favicon是favorites icon的 ...

  2. Oracle 应用于.NET平台

    1. 回顾ADO.NET ADO.NET是一组用于和数据源进行交互的面向对象类库集,它存在于.Net Framework中.通常情况下,数据源可以是各种类型的数据库,利用ADO.NET可以访问目前几乎 ...

  3. Java 代码性能优化总结

    前言 代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑的,就像大海里面的鲸鱼一样,它吃一条小虾米有用吗?没用, ...

  4. android service 的各种用法(IPC、AIDL)

    http://my.oschina.net/mopidick/blog/132325 最近在学android service,感觉终于把service的各种使用场景和用到的技术整理得比较明白了,受益颇 ...

  5. python标准模块(一)

    本文会涉及到的模块: time datetime sys os random re hashlib 模块,用若干代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能 ...

  6. 使用WebService和不使用WebService哪个速度更快哪个更安全

  7. 【浅谈html5 响应式布局之自动适应屏幕宽度】

    允许网页宽度自动调整 “自适应网页设计”到底是怎么做到的?其实并不难. 首先,在网页代码的头部,加入一行viewport元标签. <meta name=”viewport” content=”w ...

  8. 介绍ping中的TTL是什么意思

    ping是icmp报文的一种应用.用来测试网络中各设备的连通性.在这几天的实验课上,我又用到了这个非常常用的命令,但是这次我发现了一些以前没有太注意的地方,那就是我在Ping不同的地址时所返回的TTL ...

  9. jsp简单标签开发(一)

    孤傲苍狼 @Override22 public void doTag() throws JspException, IOException {23 //得到代表jsp标签体的JspFragment24 ...

  10. maven可选依赖(Optional Dependencies)和依赖排除(Dependency Exclusions)

    我们知道,maven的依赖关系是有传递性的.如:A-->B,B-->C.但有时候,项目A可能不是必需依赖C,因此需要在项目A中排除对A的依赖.在maven的依赖管理中,有两种方式可以对依赖 ...