Mondriaan's Dream (zoj 1100 状压dp)

原创 2015年11月21日 19:31:55
Mondriaan's Dream

Time Limit: 10 Seconds      Memory Limit: 32768 KB

Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan.One night, after producing the drawings in his 'toilet series' (where hehad to use his toilet paper to draw on, for all of his paper was filledwith squares and rectangles), he dreamt of filling a large rectangle withsmall 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 acomputer to calculate the number of ways to fill the large rectanglewhose dimensions were integer values, as well. Help him, so that hisdream won't turn into a nightmare!

Input Specification

The input file contains several test cases. Each test case is made up oftwo integer numbers: the height h and the width w of thelarge rectangle. Input is terminated by h=w=0. Otherwise,1<=h,w<=11.

Output Specification

For each test case, output the number of different ways the given rectanglecan be filled with small rectangles of size 2 times 1. Assume the givenlarge 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



Source: University of Ulm Local Contest 2000



/*************************************************************************
    > File Name: zoj1100_铺砖_状压.cpp
    > Author: wust_lyf
    > Mail: 2206478849@qq.com
    > Created Time: 2015年11月21日 星期六 16时39分14秒
 ************************************************************************/

/*
  题意:一个n*m的方格,要求用1*2的砖把它铺满,问有多少种铺法。
  思路:dp[i][j]表示第i行在状态j下所有合法的铺法总数。第i行的状态可以由第i-1行推得,且易知
  第i-1行的空缺只能由第i行对应位置用竖着的砖来补上。
 */

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#define eps 1e-6
#define DBG printf("Hi\n")
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = 1005;
const int MAXN = 2005;

ll dp[12][(1<<11)+10];
int n,m;

bool isok(int s)		//判断s状态能否只用横着的砖铺满
{
	for (int i=0;i<m;)
	{
		if (s&(1<<i))
		{
			if (i==m-1) return false;
			if ((s&(1<<(i+1)))==0) return false;  //第i个可以铺但第i+1个不能铺则说明这个状态不能满足
			i+=2;
		}
		else i++;
	}
	return true;
}

ll dfs(int dep,int pre)
{
	if (dep==(n+1)) return (pre==(1<<m)-1)?1:0;
	if (dp[dep][pre]!=-1) return dp[dep][pre];
	int need=((1<<m)-1)&(~pre);			//need记录为了补上一行的空缺该行要竖着放的位置
	dp[dep][pre]=0;
	for (int cur=0;cur<(1<<m);cur++)  //cur是指当前这一层要铺砖的状态
	{
		if ((cur&need)!=need) continue;		//首先判断这个状态是否包含需要竖着放的位置
		if (isok(cur^need))        //判断去掉竖着放的位置其他需要横着放的位置是否可行
			dp[dep][pre]+=dfs(dep+1,cur);
	}
	return dp[dep][pre];
}

int main()
{
	int i,j;
	while (scanf("%d%d",&n,&m))
	{
		if (n==0&&m==0) break;
		if ((n*m)%2){
			printf("0\n");
			continue;
		}
		if (n<m) swap(n,m);
		memset(dp,-1,sizeof(dp));
		printf("%lld\n",dfs(1,(1<<m)-1));
	}
    return 0;
}



poj 2411 Mondriaan's Dream(状态压缩dp)

Mondriaan's Dream Description Squares and rectangles fascinated the famous Dutch painter P...

poj2411--Mondriaan's Dream(状压dp+dfs)

Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12315...

poj2411 Mondriaan's Dream--状压dp

原题链接:http://poj.org/problem?id=2411 . 题意:一个n*m的方格,给定一个1*2的方块,要求用这个方块填充方格,填满,一共多少种填充方法。 分析:对于一...
  • LaoJiu_
  • LaoJiu_
  • 2016年10月26日 13:22
  • 265

poj 2411 Mondriaan's Dream 经典状压DP+记忆化搜索

转送门:poj 2411 Mondriaan’s Dream题目大意给出一个n*m的棋盘,及一个小的矩形1*2,问用这个小的矩形将这个大的棋盘覆盖有多少种方法。解题思路因为对应于一个方格来讲,有两种状...

Mondriaan's Dream POJ - 2411 状压DP

用2进制的01表示不放还是放 第i行只和i-1行有关 枚举i-1行的每个状态,推出由此状态能达到的i行状态 如果i-1行的出发状态某处未放,必然要在i行放一个竖的方块,所以我对上一行状态按位取反之后的...

POJ2411 Mondriaan's Dream 【状压dp】

题目链接:http://poj.org/problem?id=2411题意: 有一个n*m的矩形,有若干个2*1的小长方形,问用小长方形把这个矩形铺满的方案书,多组输入,以 0 0为结束题解: 在...

poj2411 Mondriaan's Dream(状压dp)

Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producin...

POJ 2411 Mondriaan's Dream [状压DP做法]

状压DP(多米诺骨牌问题)

POJ 2411-Mondriaan's Dream(状压DP->骨牌铺满问题)

Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17226...

poj 2411 Mondriaan's Dream(状压DP->求铺方格的方法数)@

Mondriaan's Dream Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Subm...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Mondriaan's Dream (zoj 1100 状压dp)
举报原因:
原因补充:

(最多只允许输入30个字)