题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110044#problem/A

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 题意:给了一个n*m的大矩形,用1*2的小矩形去拼这样的一个矩形,求有多少种不同的拼法,如果不能拼出这样的矩形,输出0; 思路:用二进制表示每一行的状态,每个小格中放了矩形,用1表示,没放用0表示。从第一行开始,用从0到2^m-1的二进制表示第一行的所有状态,然后初始化这所有的状态对应的dp[0][i]值,若合法赋值为1·,否则赋值为0.然后从1行开始循环判断第i行的0到2^m-1所有的状态,分别每个j状态依次对应i-1行的每个是否合法,然后若合法,dp[i][j]++。最后输出dp[n-1][2^m-1]值即为结果。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
const int MAXN=;
const int MAX=(<<)+; long long dp[MAXN][MAX];///dp[i][j]表示在第i行j状态(2进制转化为10进制)方法数(其中i-1及以上行数排列完毕)
int n,m; bool firstrow(int t)///统计能否在第一行放置t状态
{
for(int i=; i<m; )
{
if (t & (<<i))///若为1,则是横放
{
if (i==m-) return false;
if (t& (<<(i+))) i+=;///横放需要连续两个格子
else return false;
}
else i++;
}
return true;
} bool judge(int tt,int t)
{
for(int i=; i<m; )
{
if (t & (<<i))///c行的i列为1
{
if (tt & (<<i))///c-1行的i列为1,说明c行为横放
{
///横放是否合法
if ((i==m-) || !(t&(<<(i+))) || !(tt&(<<(i+))) ) return false;
else i+=;
}
else i++;///c-1行i列为0,c行i列为1,竖放
}
else
{
if (tt&(<<i)) i++;///c行i列为0,那么c-1行i列必须为1
else return false;
}
}
return true;
} void DP()
{
if (n<m)
{
///使n更大,状态数量变少
swap(n,m);
}
int max=(<<m)-;///最多的状态数max+1;
memset(dp,,sizeof(dp));
for(int i=; i<=max; i++)///第一行所有状态数
if (firstrow(i)) dp[][i]=;///第一行i状态合法置1 for(int c=; c<=n; c++)///从第二行开始dp
for(int i=; i<=max; i++)///第c行所有状态
for(int ii=; ii<=max; ii++)///第c-1行状态,因为第c行i状态是受c-1行影响的
if(judge(ii,i)) dp[c][i]+=dp[c-][ii];///如果c-1行状态与第c行状态合法,更新c行i状态方法数
printf("%lld\n",dp[n][max]);///n行max状态(均为1)方法数
} int main()
{
while(~scanf("%d%d",&n,&m) && (n || m))
{
if (n& && m&)
{
cout<<<<endl;///如果n,m同为奇数,不可能填充完全
continue;
}
DP();
}
return ;
}

状态压缩DP--Mondriaan's Dream的更多相关文章

  1. 状态压缩DP(大佬写的很好,转来看)

    奉上大佬博客 https://blog.csdn.net/accry/article/details/6607703 动态规划本来就很抽象,状态的设定和状态的转移都不好把握,而状态压缩的动态规划解决的 ...

  2. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  3. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

  4. [知识点]状态压缩DP

    // 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...

  5. HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP

    题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...

  6. DP大作战—状态压缩dp

    题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...

  7. 状态压缩dp问题

    问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...

  8. BZOJ-1226 学校食堂Dining 状态压缩DP

    1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...

  9. Marriage Ceremonies(状态压缩dp)

     Marriage Ceremonies Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  10. HDU 1074 (状态压缩DP)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...

随机推荐

  1. pecl install imagick

    steven@server:/var/www$ sudo pecl install imagickdownloading imagick-2.3.0.tgz ...Starting to downlo ...

  2. Xcode 文档注释方法

    摘自:http://www.cnblogs.com/bomo/p/4815963.html 文档注释,可以在调用时显示注释信息,让调用者更好的理解方法的用途. 注释方法: /// 注释 和 /** 注 ...

  3. [原]shader实现矩形圆角

    哎!竭力想说清楚这个实现原理,并解释清楚shader里面的算法,结果发现越解释越不好理解,见谅! 一.实现目标:矩形四角是圆弧效果 二.实现的原理:通过每个角绘制1/4圆弧,剔除掉圆弧以外的部分. 原 ...

  4. Cubieboard2裸机开发之(二)板载LED交替闪烁

    前言 电路原理在文章http://www.cnblogs.com/lknlfy/p/3583806.html中已经说明,两个LED的原理图是一样的.要使两个LED交替闪烁,只需要在点亮蓝色LED,熄灭 ...

  5. 玩转MAC OS!实测DIY兼容机装苹果系统

    1打造iMAC:DIY常规兼容机安装MAC OS回顶部 [PConline 评测]最近消息透露苹果下个月即将发布新系统MAC OS X 10.9,这是什么东西?对于苹果,留给我们印象最为深刻的是iPh ...

  6. SecureCRT使用

    SecureCRT可以说是linux远程终端的代名词,关于它的一些技巧必须掌握,,, 1.解决中文乱码 登陆主机,运行locale命令,确定语言选项LANG是否为 zh_CN.gb2312 或者 en ...

  7. 【转载】JDBC连接各种数据库的字符串

    oracle    driverClass:oracle.jdbc.driver.OracleDriver    url:jdbc:oracle:thin:@127.0.0.1:1521:dbname ...

  8. 纯CSS画的基本图形(矩形、圆形、三角形、多边形、爱心、八卦

    http://css-tricks.com/examples/ShapesOfCSS/

  9. SQL SERVER 使用select和union插入多条数据

    insert into A(A) select '2' union select '3' union select '100' go select * from A

  10. CSS3 垂直居中 左右居中

    display: -webkit-box; -webkit-box-orient: horizontal; -webkit-box-pack: center; -webkit-box-align: c ...