POJ 2411 (动态规划-状压DP AND 轮廓线DP)

悠悠 2022-05-29 00:29 343阅读 0赞

问题描述:

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.

91cd2a849209448a71d50fa577e5d5ed_v_1520837903
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. 1 2
  2. 1 3
  3. 1 4
  4. 2 2
  5. 2 3
  6. 2 4
  7. 2 11
  8. 4 11
  9. 0 0

Sample Output

  1. 1
  2. 0
  3. 1
  4. 2
  5. 3
  6. 5
  7. 144
  8. 51205

题目题意:给我们一个h*w的矩形,我们用1*2的矩形去覆盖,覆盖种数

状压DP:对于每一行我们用01二进制表示其状态1表示横放或者竖放的下面一块,0表示竖放的上面一块。

则上一行为0的地方这一行必须为1,其他的任意,还有一点上下俩行想与必须满足每一个连续的1区间的长度是偶数

很简单,只有上下为1才有是相与为1,那么都是横放,砖的长度为2偶数

代码如下:

  1. #include<iostream>
  2. #include<cmath>
  3. #include<cstdio>
  4. #include<cstring>
  5. #define ll long long
  6. using namespace std;
  7. ll dp[2][1<<12];
  8. bool vis[1<<12];
  9. int h,w;
  10. bool check(int s)
  11. {
  12. int cnt = 0;
  13. while(s){
  14. if(s&1){
  15. cnt++;
  16. }
  17. else{
  18. if(cnt&1)return false;
  19. cnt = 0;
  20. }
  21. s>>=1;
  22. }
  23. if(cnt&1)return false;
  24. return true;
  25. }
  26. void init()
  27. {
  28. memset (vis,false,sizeof (vis));
  29. for (int i=0;i<=(1<<11);i++){
  30. if (check(i)) vis[i]=true;
  31. }
  32. }
  33. int main()
  34. {
  35. init();
  36. while (scanf("%d%d",&h,&w)!=EOF) {
  37. if (h==0&&w==0) break;
  38. memset (dp,0,sizeof (dp));
  39. for (int i=0;i<(1<<w);i++) {//状态可以先处理出来
  40. if (vis[i]) dp[0][i]=1;
  41. }
  42. for (int i=1;i<h;i++) {
  43. int next=i%2;
  44. int fa=1-next;
  45. memset (dp[next],0,sizeof (dp[next]));
  46. for (int j=0;j<(1<<w);j++){
  47. for (int k=0;k<(1<<w);k++) {
  48. if (dp[fa][k]==0) continue;
  49. int cnt=((1<<w)-1)^k;//这个地方比较巧妙,想想
  50. if ((cnt&j)!=cnt) continue;
  51. if (vis[j&k]) dp[next][j]+=dp[fa][k];
  52. }
  53. }
  54. }
  55. printf("%lld\n",dp[(h-1)%2][(1<<w)-1]);
  56. }
  57. return 0;
  58. }

轮廓线DP

先看一下大佬博客点击打开链接

从一个轮廓线转移到另一轮廓线要保证处理的当前结点的上方一定要被覆盖

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. using namespace std;
  6. const double PI=acos(-1);
  7. const int MAX=1e6+100;
  8. const int MOD=1e9+7;
  9. typedef long long ll;
  10. ll d[2][1<<12];
  11. int main()
  12. {
  13. int n,m;
  14. while(scanf("%d%d",&n,&m)!=EOF)
  15. {
  16. if(n==0&&m==0)break;
  17. memset(d,0,sizeof d);
  18. int pre=0,now=1;
  19. d[pre][(1<<m)-1]=1;
  20. for(int i=0;i<n;i++)
  21. {
  22. for(int j=0;j<m;j++)
  23. {
  24. for(int k=0;k<(1<<m);k++)
  25. {
  26. if(d[pre][k]==0)continue;
  27. if((k<<1)&(1<<m))d[now][(k<<1)%(1<<m)]+=d[pre][k];//上面已经有了,不放
  28. if((k&(1<<(m-1)))==0&&i)d[now][(k<<1)^1]+=d[pre][k];//竖放
  29. if(j&&(k&1)==0&&(k&(1<<(m-1))))d[now][((k<<1)^3)%(1<<m)]+=d[pre][k];//横放而且保证当前结点的上方一定要被覆盖了
  30. }
  31. memset(d[pre],0,sizeof d[pre]);
  32. now^=1;
  33. pre^=1;
  34. }
  35. }
  36. cout<<d[pre][(1<<m)-1]<<endl;
  37. }
  38. return 0;
  39. }

发表评论

表情:
评论列表 (有 0 条评论,343人围观)

还没有评论,来说两句吧...

相关阅读

    相关 POJ 1185(动态规划-dp)

    问题描述: 司令部的将军们打算在N\M的网格地图上部署他们的炮兵部队。一个N\M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平原(用"P"表示),

    相关 HDU 5691(动态规划-dp)

    问题描述: 度度熊是他同时代中最伟大的数学家,一切数字都要听命于他。现在,又到了度度熊和他的数字仆人们玩排排坐游戏的时候了。游戏的规则十分简单,参与游戏的N个整数将会做成一排