Mondriaan's Dream
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 16771   Accepted: 9683

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

 
第一次做状压题目,没怎么优化就是暴力递推。
对于每个格子我们有这么两种可能: 
第一,这个格子被竖着的砖块覆盖
第二,这个格子被横着的砖块覆盖
我们不妨用0/1来表示这个状态,0表示这是一个竖着放置的砖块的上方部分,1表示横向覆盖或者被上方竖着覆盖。
之所以用0表示竖放的上方是因为竖着放会对下一行的放置造成影响,而横着放显然不会,我们在放置下一行的时候需要知道上一行的状态才可,
所以遇见上一行这个位置是0就表示这个格子已经被覆盖了的状态,利用这个来判断冲突放置。
(显然每个格子都要被覆盖,我们如果用01表示覆盖与非覆盖的状态显然是无意义的,要从放置方式着手,这一点当时想了好久= =)
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define LL long long
LL dp[2][2050];
LL ans[15][15];
bool check(int x,int i)
{  return x&(1<<i); }
bool comp(int A,int B,int N)
{
 int i=0,j,k;
 while(i<N){
    if(!check(A,i)){
        if(!check(B,i)) return 0;
        i++;
    }
    else{
        if(!check(B,i)) i++;
        else {
            if(i==N-1||!check(A,i+1)||!(check(A,i+1)&&check(B,i+1))) return 0;
            else i+=2;
        }
    }
 }
 return 1;
}
void solve(int N,int M)
{
 if(ans[N][M]+1) {printf("%lld\n",ans[N][M]);return;}
 memset(dp,0,sizeof(dp));
 dp[0][(1<<N)-1]=1;
 int cur=1;
 for(int i=1;i<=M;++i){
    for(int j=0;j<(1<<N);++j){dp[cur][j]=0;
        for(int k=0;k<(1<<N);++k){
            if(comp(j,k,N)) dp[cur][j]+=dp[cur^1][k];
        }
    }
    cur^=1;
 }
 ans[N][M]=ans[M][N]=dp[cur^1][(1<<N)-1];
 printf("%lld\n",dp[cur^1][(1<<N)-1]);
}
int main()
{
    memset(ans,-1,sizeof(ans));
    int N,M,i,j,k,l;
    while(scanf("%d%d",&N,&M)!=EOF&&(N||M)){
        if(N*M%2==1) {puts("0");continue;}
        if(N>M) {swap(N,M);}
        solve(N,M);  //M行N列
    }
    return 0;
}

POJ 2411 状压DP经典的更多相关文章

  1. POJ 2411 状压dp

    F - Mondriaan's Dream Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I6 ...

  2. POJ 3254 (状压DP) Corn Fields

    基础的状压DP,因为是将状态压缩到一个整数中,所以会涉及到很多比较巧妙的位运算. 我们可以先把输入中每行的01压缩成一个整数. 判断一个状态是否有相邻1: 如果 x & (x << ...

  3. poj 1170状压dp

    题目链接:https://vjudge.net/problem/POJ-1170 题意:输入n,表示有那种物品,接下来n行,每行a,b,c三个变量,a表示物品种类,b是物品数量,c代表物品的单价.接下 ...

  4. [NOI2001] 炮兵阵地 (状压Dp经典例题)

    如果您的电脑比较优秀能在 1sec 内跑过 2^1000 的时间复杂度,不妨你可以尝试一下,其实实际时间复杂度远远少于 2^1000,作为骗分不错的选择QAQ,然后我们来分析一下正解: 很显然此题是一 ...

  5. POJ 3254 状压DP

    题目大意: 一个农民有一片n行m列 的农场   n和m 范围[1,12]  对于每一块土地 ,1代表可以种地,0代表不能种. 因为农夫要种草喂牛,牛吃草不能挨着,所以农夫种菜的每一块都不能有公共边. ...

  6. poj 1185(状压dp)

    题目链接:http://poj.org/problem?id=1185 思路:状态压缩经典题目,dp[i][j][k]表示第i行状态为j,(i-1)行状态为k时最多可以放置的士兵个数,于是我们可以得到 ...

  7. poj 3254 状压dp入门题

    1.poj 3254  Corn Fields    状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...

  8. poj 3311 状压DP

    经典TSP变形 学到:1.floyd  O(n^3)处理随意两点的最短路 2.集合的位表示,我会在最后的总结出写出.注意写代码之前一定设计好位的状态.本题中,第0位到第n位分别代表第i个城市,1是已经 ...

  9. 二维状压DP经典题

    炮兵阵地 题目链接 题目大意:在n*m的地图上放置炮兵,每个炮兵的攻击范围是上下左右两格内,有两种不同的地形,山地(用"H" 表示),平原(用"P"表示),只有 ...

随机推荐

  1. 。。。。。。不带http https : 不报错 spring boot elasticsearch rest

    ......不带http https  : 不报错 先telnet http://onf:8080/getES653/道路桥梁正在“理疗”%20这14条道路纳入市政中修 @GetMapping(&qu ...

  2. 服务器和客户端的交互方式(Socket,http协议)和各自特点适用范围

    1 数据传输方式 1.1  Socket传输的定义和其特点 所谓socket通常也称作"套接字",实现服务器和客户端之间的物理连接,并进行数据传输,主要有UDP和TCP两个协议.S ...

  3. Idea的注入和自动编译配置

    实时编译: 第二个(防止编译时Autowired报错): 修改成:

  4. Linux下tomcat启动项目原因排查

    先停掉tomcat服务器: 然后把文件删除: 这时候启动服务器: 看下有没有启动成功: 接着把重新优化过的代码用X ftp传上去. 等几分钟就可以. 如果老是出现问题,就去catalina.out文件 ...

  5. MySQL的redo log结构和SQL Server的log结构对比

    MySQL的redo log结构和SQL Server的log结构对比 innodb 存储引擎 mysql技术内幕 log buffer根据一定规则将内存中的log block刷写到磁盘,这个规则是 ...

  6. sql用法

    1:  SELECT `SCHEMA_NAME`  FROM `information_schema`.`SCHEMATA`;    查询sql中的数据库名 2:  select * from for ...

  7. django-from

    构建一个表单 这是一个非常简单的表单.实际应用中,一个表单可能包含几十上百个字段,其中大部分需要预填充,而且我们预料到用户将来回编辑-提交几次才能完成操作. 我们可能需要在表单提交之前,在浏览器端作一 ...

  8. Singapore retailer will release this adidas NMD R1

    Select spots are restocking the adidas NMD Singapore this Friday, Feb 24th featuring three different ...

  9. 通过.properties配置文件,在Service层获取值

    问题:从配置文件获取不到值的原因:1.静态变量:2.没通过Spring加载该实例对象. 1. conf.properties配置文件内容: 2. Spring加载配置文件内容,spring-confi ...

  10. 用opencv检测人眼并定位瞳孔位置

    最近的研究要用到定位瞳孔的位置,所以上网搜了下相关的代码.总结如下: 1) 定位瞳孔可以直接使用opencv中的自带的分类器(haarcascade_eye_tree_eyeglasses.xml)来 ...