POJ2411: Mondriaan's Dream(1*2铺地砖,DP)

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
 
用2进制的01表示不放还是放
<p>第i行只和i-1行有关</p><p><pre name="code" class="cpp">#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<vector>
#include<cmath>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define ll long long
using namespace std;
ll dp[15][1<<11]; //表示第i行为某种状态的时候(为止)已经有多少种方法数
ll n,m,tem; //n行m列,tem表示方法数
void dfs(int i,int p,int k){ //i:第几行,p:用二进制表示的当前这一行各个位置状态,k:第几列
	if(k>=m){
		dp[i][p]+=tem;
		return;}
	dfs(i,p,k+1);
	if(k<=m-2&&!(p&1<<k)&&!(p&1<<k+1)) //如果该行连续两列都为0,则可以选择添加一个横砖
		dfs(i,p|1<<k|1<<k+1,k+2);} //p|1<<k|1<<k+1表示把该行这两个位置置1
int main(){	
	while(cin>>n>>m&&n!=0&&m!=0){
		memset(dp,0,sizeof(dp));
		tem=1;
		dfs(1,0,0);
		for(int i=2;i<=n;++i){
			for(int j=0;j<(1<<m);++j){
				if(dp[i-1][j]>0){
					tem=dp[i-1][j];
					dfs(i,~j&((1<<m)-1),0);}}} //~j&((1<<m)-1)所有位置取反
		cout<<dp[n][(1<<m)-1]<<endl;} //输出最后一行所有位置的数都是1的情况
	return 0;
}




                
L型组件填图问题 1.问题描述 设B是一个n×n棋盘,n=2k,(k=1,2,3,…)。用分治法设计一个算法,使得:用若干个L型条可以覆盖住B的除一个特殊方格外的所有方格。其中,一个L型条可以覆盖3个方格。且任意两个L型条不能重叠覆盖棋盘。 例如:如果n=2,则存在4个方格,其中,除一个方格外,其余3个方格可被一L型条覆盖;当n=4时,则存在16个方格,其中,除一个方格外,其余15个方格被5个L型条覆盖。 2. 具体要求 输入一个正整数n,表示棋盘的大小是n*n的。输出一个被L型条覆盖的n*n棋盘。该棋盘除一个方格外,其余各方格都被L型条覆盖住。为区别出各个方格是被哪个L型条所覆盖,每个L型条用不同的数字或颜色、标记表示。 3. 测试数据(仅作为参考) 输入:8 输出:A 2 3 3 7 7 8 8 2 2 1 3 7 6 6 8 4 1 1 5 9 9 6 10 4 4 5 5 0 9 10 10 12 12 13 0 0 17 18 18 12 11 13 13 17 17 16 18 14 11 11 15 19 16 16 20 14 14 15 15 19 19 20 20 4. 设计与实现的提示 对22k的棋盘可以划分成若干,每棋盘是原棋盘的子棋盘或者可以转化成原棋盘的子棋盘。 注意:特殊方格的位置是任意的。而且,L型条是可以旋转放置的。 为了区分出棋盘上的方格被不同的L型条所覆盖,每个L型条可以用不同的数字、颜色等来标记区分。 5. 扩展内容 可以采用可视化界面来表示各L型条,显示其覆盖棋盘的情况。 经典的递归问题, 这是我的大代码, 只是本人很懒, 不想再优化
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值