POJ2411 Mondriaan's Dream(状态压缩)
2016-08-05 19:51
399 查看
Mondriaan's Dream
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!
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 15295 | Accepted: 8820 |
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/* 已经给了时间看题解了,正式练习一道题三天之内不准再看题解! */ #include <iostream> #include <stdio.h> #include <string.h> #define N (1<<12)+5 #define M 12 /* 用1表示当前小方格填满了,0表示没填满,初始化第一行的状态然后递推到最后一行 */ using namespace std; long long dp[M] ;//dp[i][j]表示第i行j种状态做多有多少种排列方式 int n,m; bool check(int x)//判断是不是有连续个奇数个1;当有连续奇数个1的时候那么这一行横着放的肯定是不会填满的 { int s=0; while(x) { if(x&1)s++; else { if(s&1)return false; s=0; } x>>=1; } if(s&1)return false; return true; } int main() { ///for(int i=0;i<30;i++) // cout<<"i="<<i<<" "<<check(i)<<endl; //cout<<endl; //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while(scanf("%d%d",&n,&m)!=EOF&&n&&m) { if((n*m)%2)//如果乘积是奇数的话是肯定铺不满的 { printf("0\n"); continue; } if(n<m)//这里做一个剪枝,使m永远是小的那个,能减少不少循环的次数 { int tmp=n; n=m; m=tmp; } int tol=(1<<m); memset(dp,0,sizeof dp); for(int i=0;i<tol;i++)//初始第一行的状态 if(check(i)) dp[1][i]=1; for(int i=1;i<n;i++)//由i行推出i+1行 for(int j=0;j<tol;j++)//枚举上一个状态 if(dp[i][j]!=0)//上一个状态为零没计算意义 { for(int k=0;k<tol;k++) { if( (j|k)==tol-1 && check(j&k) )//这一步很精妙,(j|k)==tol-1保证了肯定能填满上一行,check(j&k)保证了这一行中横着放的小木块绝对能填满 dp[i+1][k]+=dp[i][j]; } } printf("%lld\n",dp [tol-1]); } return 0; }
相关文章推荐
- POJ 2299 Ultra-QuickSort (求逆序数:离散化+树状数组或者归并排序求逆序数)
- shell命令
- js,jq获取select下拉框的各项值
- 离散数学之把妹要诀
- PHP Date() 函数详细参数
- hdu 5791 dp
- HDOJ 2647 Reward
- 文件缓存与内存缓存
- Android进阶系列0—View的工作流程:measure,layout,draw小结
- Rxjava +Retrofit 你需要掌握的几个技巧,Retrofit缓存,RxJava封装,统一对有无网络处理,异常处理, 返回结果问题
- java float
- opencv 中关于BOW模型的实现以及相关的函数解释
- Rxjava +Retrofit 你需要掌握的几个技巧,Retrofit缓存,RxJava封装,统一对有无网络处理,异常处理, 返回结果问题
- 24种设计模式的体验
- QT中文处理--Qt5对付中文真好用
- android沉浸式文章集锦
- Java &和&& ||与| 的区别
- js 动态表格添加一行删除一行
- 2016多校联合训练赛 第五场1011 Two hdu5791
- QT中文处理--QString 与中文问题