首页 > ACM题库 > HDU-杭电 > HDU 1400 Mondriaan’s Dream-状态压缩-[解题报告] C++
2013
12-09

HDU 1400 Mondriaan’s Dream-状态压缩-[解题报告] C++

Mondriaan’s Dream

问题描述 :

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!

输入:

The input file 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.

输出:

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.

样例输入:

1 2
1 3
1 4
2 2
2 3
2 4
2 11
4 11
0 0

样例输出:

1
0
1
2
3
5
144
51205

题目问用1*2组成如图h*w矩形的方案有多少种。

明显的状态压缩DP,从上向下填充,假设两行的状态为stat1和stat2,则需要满足的条件是stat1|sta2=1<<w-1以及stat1&stat2中连续的1都是偶数个。因为上一行为空的下一行必须放一个竖的,而除去竖着到上一行的都是横着的,每一个横着的占的宽度都是2,所以stat1&stat2中连续的1肯定是偶数个。

对每一层要记录一个状态是否搜过,并记录这个状态有多少种方法,即记忆化搜索。复杂度是O(h*(2^w)*(2^w))。

斌牛的方法更为高效简洁,状态表示的是当前轮廓线的状态,复杂度是O(h*w*(2^w)),传送门在这里,http://www.cnblogs.com/staginner/archive/2012/09/04/2670729.html

 

#include <string.h>
 #include <stdio.h>
 #include <set>
 typedef long long LL;
 int h,w,full;
 LL d[12][2200],res[12][12];
 int doublei(int state){
     for(int i=0,d=0;i<=w;i++)
         if(i<w&&((state>>i)&1))d++;
         else if(d&1)return 0;
     return 1;
 }
 LL dfs(int state,int step){
     if(d[step][state]!=-1)return d[step][state];
     if(step==h)return state==full-1;
     d[step][state]=0;
     for(int i=0;i<full;i++){
         if((i|state)!=full-1||!doublei(state&i))continue;
         d[step][state]+=dfs(i,step+1);
     }
     return d[step][state];
 }
 int main(){
      while(scanf("%d%d",&h,&w),h||w){
         if(res[w][h])printf("%I64d\n",res[w][h]);
         else{
             full=(1<<w);
             memset(d,-1,sizeof d);
             printf("%I64d\n",res[w][h]=res[h][w]=dfs(full-1,0));
         }
     }
     return 0;
 }

解题报告转自:http://www.cnblogs.com/swm8023/archive/2012/09/04/2671158.html


  1. 第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。

  2. 问题3是不是应该为1/4 .因为截取的三段,无论是否能组成三角形, x, y-x ,1-y,都应大于0,所以 x<y,基础应该是一个大三角形。小三角是大三角的 1/4.

  3. Thanks for taking the time to examine this, I really feel strongly about it and love studying a lot more on this topic. If possible, as you acquire experience