题目网址: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. 云计算相关的一些概念Baas、Saas、Iaas、Paas

    BaaS(后端即服务:Backend as a Service)公司为移动应用开发者提供整合云后端的边界服务. SaaS(软件即服务:Software as a Service)提供了完整的可直接使用 ...

  2. 论一次iOS面试

    最近觉得现在所在公司平台用户量太少,自身技术已经到了一个瓶颈,是时候需要换一个用户量多的平台,好好研究下iOS的性能优化.内存优化等问题了. 所面试的公司由于一些默认的规定,就不多说了,大致是面了一个 ...

  3. centos7.0 手动编译 lamp环境

    首先新建用户 lamper,并添加 sodu权限 两种方法:is not in the sudoers file 解决(转) xx is not in the sudoers file 问题解决[转载 ...

  4. cmd窗口编码方式的修改

    cmd默认的编码是采用GBK   regedit HKEY_CURRENT_USER\Console\%SystemRoot%_system32_cmd.exe\CodePage 十进制:936 GB ...

  5. 数据库同步工具HKROnline SyncNavigator SQL Server互同步MySQL

    需要联系我QQ:786211180 HKROnline SyncNavigator 是一款专业的 SQL Server, MySQL 数据库同步软件.它为您提供一种简单智能的方式完成复杂的数据库数据同 ...

  6. Android Studio开发第四篇版本管理Git(下)

    前面一片介绍了在as下如何关联远程仓库,这篇就介绍在开发过程中怎么应用. 提交+Push 如果本地开发代码有改动了或者你觉得某功能做完了,你打算把改动代码提交到远程仓库,这个时候很简单, 还是在工具栏 ...

  7. Potocol Buffer详解

    protocol安装及使用 上一篇博文介绍了一个综合案例,这篇将详细介绍protocol buffer. 为什么使用protocol buffer? java默认序列化效率较低. apache的thr ...

  8. base.js

    function $_id(id){return document.getElementById(id)};//$只定义为通过ID返回元素的功能 //-----------------------do ...

  9. js数组冒泡排序,快速排序的原理以及实现

    冒泡排序: 随便从数组中拿一位数和后一位比较,如果是想从小到大排序,那么就把小的那一位放到前面,大的放在后面,简单来说就是交换它们的位置,如此反复的交换位置就可以得到排序的效果. var arr = ...

  10. LeetCode-95. Unique Binary Search Trees II

    Description: Given n, generate all structurally unique BST's (binary search trees) that store values ...