poj2411 mondriaan's dream 状压dp
来源:互联网 发布:网络维护班组优秀案例 编辑:程序博客网 时间:2024/06/16 09:05
Language:
Mondriaan's Dream
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 10925 Accepted: 6356
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!
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 21 31 42 22 32 42 114 110 0
Sample Output
10123514451205
Source
Ulm Local 2000
题意:一个m*n的矩阵,只能放1*2的木块,问将这个矩阵完全覆盖的不同放法有多少种。
思路1:横着的1*2矩阵设为1 1,竖着的1*2矩阵设为0 1。首先预处理第一行,排除奇数个连续1的情况,存储到s数组;接下来,若第i行的状态为s1,第i-1行的状态为s2,若s2合法,则s2|s1必然等于(1<<n)-1,因为不存在竖着的矩阵为 0 0;之后再判断下第i行横着放的木块是是否有奇数个连续1的情况发生,即判s[s1& s2],其中s1&s2很好的屏蔽了竖着的1。详见代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;const int MAXN=15;int s[MAXN];ll dp[MAXN][1<<MAXN];int m,n;inline bool check(int x) // 检查是否会出现奇数个连续1{ int bit=0; while(x) { if(x&1) bit++; else if(bit &1) return 0; else bit=0; x>>=1; } if(bit&1) return 0; return 1;}inline bool ok(int x,int y){ if((x|y)!=(1<<n)-1) return 0; return s[x&y];//这个很妙,将竖型的1屏蔽,判断是否会有奇数个1}void init(){ memset(s,0,sizeof(s)); memset(dp,0,sizeof(dp)); for(int i=0;i<(1<<n);i++) if(check(i)) s[i]=1;}int main(){ //freopen("text.txt","r",stdin); while(~scanf("%d%d",&n,&m) && n+m) { if((n*m)&1) { printf("0\n"); continue; } if(m<n) swap(m,n); init(); for(int i=0;i<(1<<n);i++) if(s[i]) dp[1][i]=1; for(int i=2;i<=m;i++) { for(int j=0;j<(1<<n);j++) for(int k=0;k<(1<<n);k++) if(ok(j,k)) dp[i][j]+=dp[i-1][k]; } printf("%lld\n",dp[m][(1<<n)-1]);// 选(1<<n)-1的状态是因为无论倒数第二行是竖是横,最后一行一定全是1 } return 0;}
思路2:稍后补
0 0
- poj2411 mondriaan's dream 状压dp
- poj2411--Mondriaan's Dream(状压dp+dfs)
- 状压dp Mondriaan's Dream poj2411
- poj2411 Mondriaan's Dream 状压dp
- [POJ2411] Mondriaan's Dream 状压dp
- poj2411 Mondriaan's Dream--状压dp
- POJ2411:Mondriaan's Dream(状压dp)
- POJ2411 Mondriaan's Dream 【状压dp】
- poj2411 Mondriaan's Dream(状压dp)
- poj2411 Mondriaan's Dream(状压dp)
- 【状压DP】Mondriaan's Dream POJ2411 - 基础状压
- poj2411 Mondriaan's Dream 状态压缩dp
- poj2411 Mondriaan's Dream 状态压缩dp
- poj2411 Mondriaan's Dream 插头dp做法
- POJ2411 Mondriaan's Dream(压缩DP)
- POJ2411-Mondriaan's Dream-状态dp
- 【poj2411】Mondriaan's Dream 状态压缩dp
- poj2411 Mondriaan's Dream【插头dp】
- maven2下载依赖包的同时下载其源代码包
- css hack原理
- [置顶] XMPPFrameWork IOS 开发(六)聊天室
- strtok()函数的使用心得
- android中清空所有activity栈
- poj2411 mondriaan's dream 状压dp
- OSAL跨平台设计
- JS 将变量 转化为字符串的方法, 适用于所有类型,数组, JSON对象,函数和基本类型等
- linux ubuntu 彻底删除 MYSQL 然后重装 MYSQL
- 用例图总结
- VS2005调试MFC程序时出现Runtime error的解决办法
- 没能形成子女被低估vksdkjbh
- 模块管理常规功能自定义系统的设计与实现(30--第二阶段总结)
- [置顶] XMPPFrameWork IOS 开发(七)消息回执