POJ2411 Mondriaan's Dream --- 状压dp

题目链接 http://poj.org/problem?id=2411

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

Source

Ulm Local 2000

题目大意:

在nxm的方格纸上,用1x2的矩形块覆盖方格纸,要求矩形块不能有重叠,并且要刚好完全将纸覆盖,求有多少种覆盖方法。

题解:

经典的状压dp入门题,解法参考自 https://blog.csdn.net/u013480600/article/details/19569291 

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
int n,m;
int path[5000000][2];// 存储所有可能情况 
int num;// path数组长度 
ll dp[15][3000];

// 计算所有可能的path , 1->m列 
void CalPath(int col,int pre,int now) {	
	if(col > m) return;// 不加此行答案是错的,因为在递归的时候会有col+2这种操作使得col越界 
	if(col >= m) {
		path[num][0] = pre;// 前一行状态 
		path[num++][1] = now;// 后一行状态 
		return;
	}
	CalPath(col+1,pre<<1,now<<1|1); 
	CalPath(col+1,pre<<1|1,now<<1);
	CalPath(col+2,pre<<2|3,now<<2|3);	
}



int main()  {
		
	while(scanf("%d%d",&n,&m) == 2) {
		if(n == 0 && m == 0) break;
		//if(n > m) swap(n,m);
		memset(path,0,sizeof(path));
		memset(dp,0,sizeof(dp));
		num = 0;
		CalPath(0,0,0);
		// 初始化第0行
		dp[0][(1<<m)-1] = 1;
		for(int i = 1;i <= n;i++) 
			for(int j = 0;j < num;j++) {
				dp[i][path[j][1]] += dp[i-1][path[j][0]];
			}
		
		printf("%lld\n",dp[n][(1<<m)-1]);
	}
			
	return 0;
}

 

The girl's dream

02-24

[img=http://hi.csdn.net/attachment/201102/25/2306100_1298595041HR4q.png][/img]rn[color=#FF99CC]女孩rn曾经有一个梦想rn独自背着行囊去流浪rn无论天涯海角rn都要留下属于她的足迹rn去看看蓝蓝的大海rn去聆听一下大海的呼唤rn那将是女孩的第一个驿站rn要去寻找大海的女儿rn要去寻找人鱼的传说[/color]rn[img=http://hi.csdn.net/attachment/201102/25/2306100_1298595041X4Ka.png][/img]rn女孩rn曾经有一个梦想rn背着蝶儿的翅膀rn追随着蝶儿的舞步rn无忧的飞舞在蓝天下rn呼吸着自由的空气rn俯瞰着青春的脚步rn用岁月去描绘七色的未来rn用时光去撰写五彩的童话rn[img=http://hi.csdn.net/attachment/201102/25/2306100_1298595041V77G.png][/img]rn女孩rn曾经有一个梦想rn奔跑在绿色的原野上rn为风儿送去幸福的微笑rn为云儿披上绚丽的霓裳rn缭绕在她身边那多情的蝶儿rn伴随着女孩一起rn畅游在无尽的希望里rn去寻找梦中的天堂rn[img=http://hi.csdn.net/attachment/201102/25/2306100_1298595041Bcf6.png][/img]rn女孩rn曾经有一个梦想rn期待着玫瑰花开的时节rn依偎在爱的怀抱里rn倾听着花儿的低语rn低下头望着手中的rn那一抹羞涩rn她将会是谁眼中的迷恋rn低下头问一问手中的rn那一丝妩媚rn她将会为了谁而绽放rn[img=http://hi.csdn.net/attachment/201102/25/2306100_12985950421172.png][/img]rn女孩rn曾经有一个梦想rn在花瓣雨漫天飘落的日子rn为自己演奏朦胧的乐章rn和着缠绵动人的音符rn伴着飞花窃窃的私语rn肆意撩拨着青涩的琴弦rn此时的女孩应该是醉的rn只为那徘徊在她心头rn甜美的爱情rn[img=http://hi.csdn.net/attachment/201102/25/2306100_1298595042JvyP.png][/img]rn女孩rn曾经有一个梦想rn一个人静静地坐在相思湖畔rn感受着空气中弥漫的花香rn悠然的采拾着飘落身旁rn那依旧娇艳的黄花rn不愿意让尘世的污浊rn覆盖着她们洁净的魂灵rn轻轻的问一声那寂静的湖水rn可曾愿意珍藏她们的容颜rn[img=http://hi.csdn.net/attachment/201102/25/2306100_1298595042bw9g.png][/img]rn女孩rn曾经有一个梦想rn提着为自己点亮的心灯rn在迷失的森林里前进rn猜测着蝶儿的私语rn驱赶着黑暗的恐惧rn就让手中这微弱的灯光rn照亮旅途中的灰暗rn远离红尘的所有纷扰rn宁愿这条路没有尽头rn[img=http://hi.csdn.net/attachment/201102/25/2306100_129859504298OR.png][/img]rn女孩rn曾经有一个梦想rn为自己折叠一只小船rn承载着无数的幻想和期盼rn扑捉着蝶儿的痕迹rn在茫茫心海里漂泊rn寻寻觅觅着停靠的港湾rn风浪中陪着小船一起成长rn就这样安静的去守侯rn就这样孤独的去等待rn再不问归航rn[img=http://hi.csdn.net/attachment/201102/25/2306100_1298595042r2tT.png][/img]rn女孩rn曾经的希望很多rn可是如今的女孩rn却无言的伫立在雨中rn任凭那无情的雨滴打落rn那身影是如此的寂寥与孤单rn女孩闭上双眼是在祈祷吗rn还是在缅怀那渐渐远去的青春rnrn文章来源:[url=http://www.askcici.com/girls-dream/][/url]rnrnrnrn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试