POJ 2411 Mondriaan's Dream [状压DP做法]
来源:互联网 发布:计算机二级vb视频教程 编辑:程序博客网 时间:2024/06/05 16:05
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
状压DP~
状态的记录方式:对于横放,两个格子都为1,对于竖放,上一个格子为0,下一个格子为1,这样可以方便地判断上下两行的状态是否符合要求。
用f[i][k]表示状态k时,第i行的种类数,通过枚举i-1行的情况来更新,主要就是判断上一行和这一行是否符合。
判断方式:
分为第一行和其余所有行,第一行判断是否有连续的奇数个0,如果有,就不符合情况。
其余所有行,假设判断到i行的j位,若[i][j]=0且上一行j位也为0,则不符合;如果[i][j]=0且上一行j位为1,则这一位符合,i++;
若[i][j]=1且上一行j为0,则这一位符合,i++;若[i][j]=1且上一行j为1,则表示这一行j+1位必为1,如果不是,就不符合;如果是,再分类讨论i-1行j+1位是否为1,如果不是1,则也不符合;如果是1,则i直接加2。
(限于语言表达能力,没看懂上面一段的还请看代码QAQ)
最后一行肯定是全是1,所以直接用(1<<m)-1来判断,更新ans即可,不用从0循环到(1<<m)-1,可以大大减少搜索时间。
但是为什么交换nm结果就错了啊……好奇怪啊……
(这道题也能用插头DP做,16ms,详见http://blog.csdn.net/senyelicone/article/details/56830274)
#include<cstdio>#include<cstring>#include<iostream>using namespace std;#define ll long longint n,m,tot;ll f[11][2050],ans;bool chec(int u,int uv,int vv){if(u==1){int i=0;while(i<m){if(!(uv&(1<<i))) i++;else if(i==m-1 || !(uv&(1<<(i+1)))) return 0;else i+=2;}return 1;}int i=0;while(i<m){if(!(uv&(1<<i))){if(!(vv&(1<<i))) return 0;i++;}else if(!(vv&(1<<i))) i++;else if(i==m-1 || !((uv&(1<<(i+1))) && (vv&(1<<(i+1))))) return 0;else i+=2;}return 1;}int main(){while(scanf("%d%d",&n,&m)==2 && n){if((n*m)%2){printf("0\n");continue;}if(n==1){printf("1\n");continue;}memset(f,0,sizeof(f));ans=0;tot=(1<<m)-1;for(int i=0;i<=tot;i++) if(chec(1,i,0)) f[1][i]=1;for(int i=2;i<n;i++) for(int k=0;k<=tot;k++) for(int j=0;j<=tot;j++) if(f[i-1][j] && chec(i,k,j)) f[i][k]+=f[i-1][j];for(int i=0;i<=tot;i++) if(chec(n,tot,i)) ans+=f[n-1][i];printf("%lld\n",ans);}return 0;}
1 0
- POJ 2411 Mondriaan's Dream [状压DP做法]
- POJ 2411 Mondriaan's Dream [插头DP做法]
- POJ 2411 Mondriaan's Dream (状压DP)
- Mondriaan's Dream - POJ 2411 状压dp
- POJ 2411 Mondriaan's Dream (状压DP)
- poj 2411 Mondriaan's Dream 状压dp
- poj 2411 Mondriaan's Dream(状压dp)
- POJ 2411 Mondriaan's Dream 状压dp
- POJ 2411 Mondriaan's Dream ( 状压DP )
- Mondriaan's Dream POJ - 2411 状压DP
- POJ 2411 Mondriaan's Dream (状压dp)
- POJ 2411 Mondriaan's Dream 状压dp
- [POJ 2411]Mondriaan's Dream:状压DP
- POJ 2411 Mondriaan's Dream (状压DP)
- poj 2411 Mondriaan's Dream 【状压dp】
- poj 2411 Mondriaan's Dream dp
- poj 2411 Mondriaan's Dream 【dp】
- POJ 2411 Mondriaan's Dream(状压DP)
- 天线的基本参数介绍
- AS2.2.3新建工程出现的问题(直接贴了代码,耐心比对不同之处)
- 交换机开发(一)—— 交换机的工作原理
- 线性表的顺序存储
- 455. Assign Cookies
- POJ 2411 Mondriaan's Dream [状压DP做法]
- HDOJ 1005 Java 答案
- 深度学习caffe实战(一)验证码识别
- python解析基于xml格式的日志文件
- codeforces 399 E. Game of Stones 博弈 思维题
- javascript Object.is 与 === 的区别
- 昇润科技将在三月底推出CC2640R2 SDK开发平台
- 可以设置宽高比,宽度确定,高度比例缩放的ImageView快速实现
- JVM调优总结(十)-调优方法