题目网址: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. PL-SQL 存储函数和存储过程

     PL-SQL 存储函数和存储过程 ORACLE 提供能够把PL/SQL 程序存储在数据库中,并能够在不论什么地方来执行它.这样就叫存储过程或函数. 过程和函数统称为PL/SQL子程序.他们是被命 ...

  2. Django模块学习- django-pagination

    实在是很简单的一个Django 的分页插件. 使用pip instal pagination 即可完成安装. 完成后配置如下: 1. 将安装文件中的 pagination 文件夹拷贝到项目的根目录下 ...

  3. Rxlifecycle(一):使用

    Rxlifecycle使用非常方便简单,如下: 1.集成 build.gradle添加 //Rxlifecycle compile 'com.trello:rxlifecycle:0.3.1' com ...

  4. 利用VBA查找excel中一行某列第一次不为空与最后一列不为空的列数

    昨日同事有需求,想知道每个商品第一次销售的月份,以及最后一次销售的月份. 本想通过什么excel函数来解决,但是找了半天也没找到合适的,最后还是通过VBA来解决吧. 使用方法: Excel工具-宏-V ...

  5. saiku 元数据存储分析

    一.介绍 使用saiku的人一定对他的元数据存储都特别感兴趣,特别是有分布式管理需求的项目,更是迫切需要了解.其实它是使用Apache的开源项目Jackrabbit管理文件的! 二.代码跟踪 我也是使 ...

  6. Microsoft 2013 新技术学习笔记 一

    有几年没有关注技术了,最近有点时间想把技术重新捡起来,借着重构手上的一个后台管理框架的机会将微软新的几种技术全部应用一下,从目的上来讲并没有希望能对涉及的技术有很深入的了解,所以这个系列的文章(篇幅不 ...

  7. Apache MPM winnt

    Win32DisableAcceptEx 指令在 apache2.4 被 AcceptFilter None 取代

  8. POJ 1631 Bridging signals

    Bridging signals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9441   Accepted: 5166 ...

  9. 移动端前端框架UI库(Frozen UI、WeUI、SUI Mobile)

    Frozen UI 自述:简单易用,轻量快捷,为移动端服务的前端框架. 主页:http://frozenui.github.io/ 开发团队:QQVIP FD Team Github:https:// ...

  10. css3背景颜色渐变

    <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...