zoukankan      html  css  js  c++  java
  • 「POJ」 2411 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. 

    NxM的瓷地板,用1x2和2x1的瓷砖铺,不能重合,求总方案数,1<=N,M<=11。多组数据询问,0 0结束。

    做法:(详见行内注释)

    看数据范围想到状压dp,dp[i][s]表示铺到第i行时此行的状态。

    dfs情况:
    (1).dfs (k+1,now<<1,(last<<1)|1);
    不铺的情况,即本行被占领,仅在(last<<1)|1成立,直接右移
    (2).dfs (k+1,(now<<1)|1,last<<1);
    竖铺的情况,在本行不被占领情况下,新的状态为(now<<1)|1
    (3).dfs (k+2,(now<<2)|3,(last<<2)|3);
    横铺的情况,在本行两个位置都不被占领的情况下,即(last<<2)|3,新的状态为(now<<2)|3

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    long long h,w,i,digit;
    long long dp[20][1<<15];
    
    //竖着铺
    void dfs(int k,int now,int last)
    {
        if(k>w) return;
        if(k==w)
        {
            dp[i][now]+=dp[i-1][last];
            
        }
        /*if((last<<1)|1==1) */dfs(k+1,now<<1,(last<<1)|1); //该行被占用,标记,skip!
        /*if((last<<1)|1!=1) */dfs(k+1,(now<<1)|1,last<<1);//该行未被占用,标记占用后带走
        /*if((last<<2)|3==1) */dfs(k+2,(now<<2)|3,(last<<2)|3);
        return;
    }
    
    int main()
    {
        while(cin>>h>>w)
        {
            if(h==0&&w==0) return 0;
            if(h==0||w==0) 
            {
                cout<<0<<endl;
                continue;
            }
            if(h%2==1&&w%2==1) 
            {
                cout<<0<<endl;
                continue;
            }
            if(h<w) swap(h,w);//前面是竖着铺所以这样更快
            memset(dp,0,sizeof(dp));
            // digit=(1<<w)-1;
            dp[0][(1<<w)-1]=1;
            for(i=1;i<=h;i++) dfs(0,0,0);
            cout<<dp[h][(1<<w)-1]<<endl;
        }
    }
  • 相关阅读:
    Jmeter响应断言的处理。
    Jmeter超时处理。
    HTTP协议简介以及特点。
    自动化测试面试技巧。
    父类构造方法有无参数对子类的影响。
    自动化分层思想分析1.
    设计模式
    遍历课程列表代码。
    如何遍历当前页课程定位分析,以及代码编写。
    “笨方法”学习Python笔记(1)-Windows下的准备
  • 原文地址:https://www.cnblogs.com/LocaEtric/p/9614816.html
Copyright © 2011-2022 走看看