zoukankan      html  css  js  c++  java
  • 状态压缩DP--Mondriaan's Dream

    题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110044#problem/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.

    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


    题意:给了一个n*m的大矩形,用1*2的小矩形去拼这样的一个矩形,求有多少种不同的拼法,如果不能拼出这样的矩形,输出0;

    思路:用二进制表示每一行的状态,每个小格中放了矩形,用1表示,没放用0表示。从第一行开始,用从0到2^m-1的二进制表示第一行的所有状态,然后初始化这所有的状态对应的dp[0][i]值,若合法赋值为1·,否则赋值为0.然后从1行开始循环判断第i行的0到2^m-1所有的状态,分别每个j状态依次对应i-1行的每个是否合法,然后若合法,dp[i][j]++。最后输出dp[n-1][2^m-1]值即为结果。

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    using namespace std;
    const int MAXN=15;
    const int MAX=(1<<11)+10;
    
    long long 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;///最多的状态数max+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行状态,因为第c行i状态是受c-1行影响的
        if(judge(ii,i)) dp[c][i]+=dp[c-1][ii];///如果c-1行状态与第c行状态合法,更新c行i状态方法数
        printf("%lld
    ",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;
    }
  • 相关阅读:
    mysql 常用命令行
    mysql常用命令
    Mac os安装wget
    linux下给文件夹或者目录赋权
    Python学习相关资料
    Mac常用的一些操作
    Mac os安装git及 git及githup的使用
    Linux磁盘占用100%解决方法
    page-break-after:always
    工具
  • 原文地址:https://www.cnblogs.com/chen9510/p/5320742.html
Copyright © 2011-2022 走看看