POJ 2663 Tri Tiling

题目链接:http://poj.org/problem?id=2663


题意:用1×2的砖铺满3×n的矩形,问一共有多少种铺法。

思路:状态压缩DP, 只有两种铺法,横着铺和竖着铺。我们按行dp,那么就要把每一行的状态表示出来。对于横着铺,可以用两个相邻的1来表示;对于竖着放,我们将上面那行的位置记0,下面那行的位置记1,也就是竖着的0 1表示。先确定第一行的状态(按照状态规定,第一行的1都是横着放得来的)所以如果存在相邻的1,它们的数量必定是偶数个。接着我们枚举相邻两行的状态进行转移,当前行s1,上一行s2:s1放完的时候s2应该也被填充满了,所以(s1 | s2) 一定是满的。其次再考虑两行的兼容问题,(s1 & s2)为第一行的可行状态时才兼容,因为所有竖着放的位置会被忽略(1 & 0),只剩下横着放的位置。所以最后的答案应该是dp[n][(1<<3)-1],最后一行一定要放满。


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;

#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)

#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 0x7fffffff
#define mod %100000007

int n;

LL dp[35][8];

bool fit(int x1 , int x2 , int uplim)
{
    if ( (x1 | x2) != uplim ) return false;
    int t = x1 & x2;
    if ( t == 0 || t == 3 || t == 6 ) return true;
    return false;
}

int main()
{
    while( ~scanf("%d",&n) )
    {
        if ( n == -1 ) break;
        if ( n == 0 )
        {
            puts("1");
            continue;
        }
        if ( n & 1 )
        {
            puts("0");
            continue;
        }
        Clean(dp,0);
        dp[1][0] = dp[1][3] = dp[1][6] = 1;
        int uplim = 1<<3;
        rep(i,2,n)
            rep(j,0,uplim-1)
                rep(k,0,uplim-1)
                 if( fit(j,k,uplim-1) ) dp[i][j]+=dp[i-1][k];
        cout<<dp[n][uplim-1]<<endl;
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值