题目网址: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. pod 出错备忘

    pod install #输出信息 /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems ...

  2. u3d动态加入模型

    楼层一层一层的加,把模型分开,弄成prefab放到Resourse文件夹里,在代码里用Instantiate(Resources.Load("模型名字") as GameObjec ...

  3. java 使用 ScriptEngineManager 解析逻辑表达式

    将表达式替换成js使用的文本格式.然后带入eval函数. public class JieXi { public static void main(String[] args) throws Exce ...

  4. listview 设置数组为空

    listview.setEmpty(View view); 使用listView或者gridView时,当列表为空时,有时需要显示一个特殊的empty view来提示用户,今日对这个方法进行一下小结, ...

  5. C/C++/Qt 统计运行时间

    http://www.cnblogs.com/Romi/archive/2012/04/19/2457175.html 程序中经常需要统计时间,需要统计某项运算的运行时间时,需要计算时间差. 1. C ...

  6. POJ 1308 Is It A Tree?

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18778   Accepted: 6395 De ...

  7. oracle数据库的字符集更改

    A.oracle server 端 字符集查询  select userenv('language') from dual 其中NLS_CHARACTERSET 为server端字符集 NLS_LAN ...

  8. CG

    //设置线的宽度 CGContextSetLineWidth(ctx, 12); //设置线的连接处 (拐点) CGContextSetLineJoin(ctx, kCGLineJoinRound ) ...

  9. 金蝶EAS常用表

    select * from T_SCM_BillType where FName_L2 like '%委外%'   --单据类型表,查业务单据对应的表  102--销售出库单  330--应收单 se ...

  10. SNF开发平台WinForm之四-开发-主细表管理页面-SNF快速开发平台3.3-Spring.Net.Framework

    4.1运行效果: 4.2开发实现: 4.2.1          有了第一个程序的开发,代码生成器的配置应该是没有问题了,我们只要在对应的数据库中创建我们需要的表结构就可以了,如下: 主表结构如下: ...