POJ2411 Mondriaan's Dream(压缩DP)
2016-07-18 09:54
489 查看
Mondriaan's Dream
DescriptionSquares 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.
![](http://poj.org/images/2411_1.jpg)
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!InputThe 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.OutputFor 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.
![](https://img-blog.csdn.net/20160718095715421)
Sample Input1 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
压缩DP;横的用11表示,竖的用法01表示。合法的状态应该是相邻两行的或是全1,与的话不存在连续的1的个数是奇数的。
关于程序算法艺术与实践更多讨论与交流,敬请关注本博客和新浪微博songzi_tea.
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 8974 | Accepted: 5187 |
![](http://poj.org/images/2411_1.jpg)
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!InputThe 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.OutputFor 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 Input1 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
压缩DP;横的用11表示,竖的用法01表示。合法的状态应该是相邻两行的或是全1,与的话不存在连续的1的个数是奇数的。
/* * 状态压缩DP一个h*w的矩阵(1<=h,w<=11),只能放1*2的模块,问完全覆盖的不同放发有多少种? * 横着的定义为11,竖着的定义为01,然后按行DP,两行状态相或要全为1.两行相与要没有连续的1的个数是奇数个 */ #include <iostream> #include <string.h> #include <stdio.h> #include <algorithm> using namespace std; bool check(int s)//判断s有没有奇数个连续的1 { int ret=0; while(s) { if(s&1)ret++; else { if(ret&1)return false; ret=0; } s>>=1; } if(ret&1)return false; return true; } long long dp[12][2050]; int main() { int n,m; while(scanf("%d%d",&n,&m)==2) { if(n==0 && m==0)break; int tot=(1<<m); memset(dp,0,sizeof(dp)); for(int i=0;i<tot;i++) if(check(i)) dp[1][i]=1; for(int i=1;i<n;i++) for(int j=0;j<tot;j++) if(dp[i][j]!=0) { for(int k=0;k<tot;k++) if( (j|k)==tot-1 && check(j&k) ) dp[i+1][k]+=dp[i][j]; } printf("%I64d\n",dp [tot-1]); } return 0; }
关于程序算法艺术与实践更多讨论与交流,敬请关注本博客和新浪微博songzi_tea.
相关文章推荐
- C++动态规划之最长公子序列实例
- C++动态规划之背包问题解决方法
- C#使用动态规划解决0-1背包问题实例分析
- 动态规划
- C++ 动态规划
- 动态规划解决背包问题的核心思路
- DP(动态规划) 解游轮费用问题
- 动态规划的用法——01背包问题
- 动态规划的用法——01背包问题
- 《收集苹果》 动态规划入门
- 《DNA比对》蓝桥杯复赛试题
- 《背包问题》 动态规划
- 自顶向下动态规划解决最长公共子序列(LCS)问题
- 01背包问题
- 初学ACM - 半数集(Half Set)问题 NOJ 1010 / FOJ 1207
- 关于爬楼梯的动态规划算法
- 动态规划 --- hdu 1003 **
- DP问题各种模型的状态转移方程
- 0-1背包解题过程
- 背包问题