poj2411状态压缩dp训练2——A

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.

poj2411状态压缩dp训练2——A_第1张图片

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

有位大神已经把博客写的很好了,以前没怎么做过状态压缩dp,是个新手,啥都不会,这道题是大神给我讲的,我也是按照他的思路做的,基本一样,自己没有什么高见,只好借鉴他山之石,以铺我脚之路矣;

自己也没啥补充的;QAQ


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const double eps = 1e-4;
const int MAXN=15;
const int MAX=(1<<11)+10;

ll dp[MAXN][MAX];///dp[i][j]表示在第i行j状态(2进制转化为10进制)方法数(其中i-1及以上行数排列完毕)
int n,m;

bool firstrow(int t)///统计能否在第一行放置t状态
{
    for(int i=0; i<m; )
    {
        if (t & (1<<i))///若为1,则是横放
        {
            if (i==m-1) return false;
            if (t& (1<<(i+1))) i+=2;///横放需要连续两个格子
            else return false;
        }
        else i++;
    }
    return true;
}

bool judge(int tt,int t)
{
    for(int i=0; i<m; )
    {
        if (t & (1<<i))///c行的i列为1
        {
            if (tt & (1<<i))///c-1行的i列为1,说明c行为横放
            {
                ///横放是否合法
                if ((i==m-1) || !(t&(1<<(i+1))) || !(tt&(1<<(i+1))) ) return false;
                else i+=2;
            }
            else i++;///c-1行i列为0,c行i列为1,竖放
        }
        else
        {
            if (tt&(1<<i)) i++;///c行i列为0,那么c-1行i列必须为1
            else return false;
        }
    }
    return true;
}

void DP()
{
    if (n<m)
    {
      ///使n更大,状态数量变少
      swap(n,m);
    }
    int max=(1<<m)-1;///最多的状态数
    memset(dp,0,sizeof(dp));
    for(int i=0; i<=max; i++)///第一行所有状态数
    {
        if (firstrow(i)) dp[1][i]=1;///第一行i状态合法置1
    }
    for(int c=2; c<=n; c++)///从第二行开始dp
    {
        for(int i=0; i<=max; i++)///第c行所有状态
        {
            for(int ii=0; ii<=max; ii++)///第c-1行状态,因为第i行状态是受c-1行影响的
            {
                if(judge(ii,i)) dp[c][i]+=dp[c-1][ii];///如果c-1行状态与第c行状态合法,更新c行i状态方法数
            }
        }
    }
    printf("%lld\n",dp[n][max]);///n行max状态(均为1)方法数
}

int main()
{
    while(~scanf("%d%d",&n,&m) && (n || m))
    {
        if (n&1 && m&1)
        {
            cout<<0<<endl;    ///如果n,m同为奇数,不可能填充完全
            continue;
        }
        DP();
    }
    return 0;
}
///891ms
有一个流氓代码,只需要0ms;;
</pre><pre code_snippet_id="1621908" snippet_file_name="blog_20160324_5_7121167" name="code" class="cpp"><pre name="code" class="cpp">#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

using namespace std;
long long f[20][20];
int main()
{
    int n,m;
//freopen("t.txt", "r", stdin);
    f[1][1] =0;
    f[1][2] =1;
    f[1][3] =0;
    f[1][4] =1;
    f[1][5] =0;
    f[1][6] =1;
    f[1][7] =0;
    f[1][8] =1;
    f[1][9] =0;
    f[1][10] =1;
    f[1][11] =0;
    f[2][1] =1;
    f[2][2] =2;
    f[2][3] =3;
    f[2][4] =5;
    f[2][5] =8;
    f[2][6] =13;
    f[2][7] =21;
    f[2][8] =34;
    f[2][9] =55;
    f[2][10] =89;
    f[2][11] =144;
    f[3][1] =0;
    f[3][2] =3;
    f[3][3] =0;
    f[3][4] =11;
    f[3][5] =0;
    f[3][6] =41;
    f[3][7] =0;
    f[3][8] =153;
    f[3][9] =0;
    f[3][10] =571;
    f[3][11] =0;
    f[4][1] =1;
    f[4][2] =5;
    f[4][3] =11;
    f[4][4] =36;
    f[4][5] =95;
    f[4][6] =281;
    f[4][7] =781;
    f[4][8] =2245;
    f[4][9] =6336;
    f[4][10] =18061;
    f[4][11] =51205;
    f[5][1] =0;
    f[5][2] =8;
    f[5][3] =0;
    f[5][4] =95;
    f[5][5] =0;
    f[5][6] =1183;
    f[5][7] =0;
    f[5][8] =14824;
    f[5][9] =0;
    f[5][10] =185921;
    f[5][11] =0;
    f[6][1] =1;
    f[6][2] =13;
    f[6][3] =41;
    f[6][4] =281;
    f[6][5] =1183;
    f[6][6] =6728;
    f[6][7] =31529;
    f[6][8] =167089;
    f[6][9] =817991;
    f[6][10] =4213133;
    f[6][11] =21001799;
    f[7][1] =0;
    f[7][2] =21;
    f[7][3] =0;
    f[7][4] =781;
    f[7][5] =0;
    f[7][6] =31529;
    f[7][7] =0;
    f[7][8] =1292697;
    f[7][9] =0;
    f[7][10] =53175517;
    f[7][11] =0;
    f[8][1] =1;
    f[8][2] =34;
    f[8][3] =153;
    f[8][4] =2245;
    f[8][5] =14824;
    f[8][6] =167089;
    f[8][7] =1292697;
    f[8][8] =12988816;
    f[8][9] =108435745;
    f[8][10] =1031151241;
    f[8][11] =8940739824;
    f[9][1] =0;
    f[9][2] =55;
    f[9][3] =0;
    f[9][4] =6336;
    f[9][5] =0;
    f[9][6] =817991;
    f[9][7] =0;
    f[9][8] =108435745;
    f[9][9] =0;
    f[9][10] =14479521761;
    f[9][11] =0;
    f[10][1] =1;
    f[10][2] =89;
    f[10][3] =571;
    f[10][4] =18061;
    f[10][5] =185921;
    f[10][6] =4213133;
    f[10][7] =53175517;
    f[10][8] =1031151241;
    f[10][9] =14479521761;
    f[10][10] =258584046368;
    f[10][11] =3852472573499;
    f[11][1] =0;
    f[11][2] =144;
    f[11][3] =0;
    f[11][4] =51205;
    f[11][5] =0;
    f[11][6] =21001799;
    f[11][7] =0;
    f[11][8] =8940739824;
    f[11][9] =0;
    f[11][10] =3852472573499;
    f[11][11] =0;
    while (scanf("%d%d", &n, &m), (n | m) >0)
        printf("%lld\n", f[n][m]);
    return 0;
}





你可能感兴趣的:(poj2411状态压缩dp训练2——A)