Mondriaan's Dream
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 18096   Accepted: 10357

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



Sample Output



题意:


给出n * m的棋盘,问用1 * 2的骨牌铺满棋盘的方案数。

分析:


棋盘n,m很小,可以想到状压dp。一般的状压dp是枚举上一维的状态和当前这维状态然后转移。

在蓝书上P384页,也有一种解法。但是网上有另一种做法:http://blog.csdn.net/sf____/article/details/15026397

十分感谢博主的思路。

思路是这样的:

依然定义f[i][j][k],i为第i行,j为第第j列。k为二进制数,1 - k - 1位为当前行状态,k - m 为上一行状态,当前更新把第k位从上一行更新成当前行状态。

二进制中0表示下一行这个位置可以放数(即当前位置不放或者横着放),1表示下一行这个位置不可以放数(即当前位置竖着放)

可以得到dp状态:

dp[i][j][k ^ (1 << j)] += dp[i][j - 1][k]; -- 1 //竖着放 或者不放,因为不可能连续两行不放,所以k ^ (1 << j)和k相同位置必须有一位为1

dp[i][j][k ^ (1 << (j - 1))] += dp[i][j - 1][k]; --2 //从前一格竖着放的转移到当前位置横着放的 条件:当前这位上一格必须放了

因为i 和 j其实是刷表的,可以转移成dp[2][k];就可以了

AC代码:


# include <iostream>
# include <cstdio>
# include <cstring>
using namespace std;
const int N = << ;
long long dp[][N];
int n,m,data;
int main(){
while(~scanf("%d %d",&m,&n) && (n + m)){
data = ( << m);
if(m > n)swap(n,m);
int now = ;
memset(dp[now],,sizeof dp[now]);
dp[now][] = ;
for(int i = ;i < n;i++){
for(int j = ;j < m;j++){
now ^= ;
memset(dp[now],,sizeof dp[now]);
for(int k = ;k < data;k++)if(dp[now ^ ][k]){
dp[now][k ^ ( << j)] += dp[now ^ ][k];
if(j && (k & ( << (j - ))) && !(k & ( << j)))
dp[now][k ^ ( << (j - ))] += dp[now ^ ][k];
}
}
}
printf("%lld\n",dp[now][]);
}
}

最新文章

  1. Android 手机卫士--平移动画实现
  2. Zend Studio XDebug调试配置
  3. 安卓开发_慕课网_Fragment实现Tab(App主界面)
  4. java 中的一个项目如何做到访问另一个项目的一个方法 或者 页面
  5. Web API路由和动作选择
  6. Elasticsearch学习笔记
  7. [转]unable to resolve superclass of 的奇怪问题和一种解决方法!
  8. bzoj 1879 [Sdoi2009]Bill的挑战(状压DP)
  9. python 各模块
  10. JavaScript 中实现继承的方式(列举3种在前一章,我们曾经讲解过创建类的最好方式是用构造函数定义属性,用原型定义方法。)
  11. Tomcat8安装及配置教程
  12. SQL server2005学习笔记(一)数据库的基本知识、基本操作(分离、脱机、收缩、备份、还原、附加)和基本语法
  13. python copy模块
  14. 设计模式のMementoPattern(备忘录模式)----行为模式
  15. Win7开机卡在Windows Update 35%的解决办法
  16. React文档(十二)组合vs继承
  17. python六十三课——高阶函数之sorted
  18. warning C4996: &#39;strcpy&#39;: This function or variable may be unsafe.
  19. Python中print/format字符串格式化实例
  20. 搭建Modelsim SE仿真环境-使用do文件仿真

热门文章

  1. K-means算法Java实现
  2. SVN与TFS自动同步脚本(很实用)
  3. vue+element ui项目总结点(一)select、Cascader级联选择器、encodeURI、decodeURI转码解码、mockjs用法、路由懒加载三种方式
  4. JDO
  5. C++中vector用法
  6. vue render {} 对象 说明文档
  7. android 近百个源码项目【转】
  8. No-8.其他命令
  9. web中的$多种意思
  10. 关于inet_ntop、inet_pton中的n和p分别代表的意义