广场铺砖问题

来源:互联网 发布:日本看中国网络语言 编辑:程序博客网 时间:2024/06/10 01:42

广场铺砖问题


Description

有一个 WH 列的广场,需要用 12 小砖铺盖,小砖之间互相不能重叠,问有多少种不同的铺法?


Input

只有一行 2 个整数,分别为 WH,(1W,H11


Output

只有 1 个整数,为所有的铺法数。


Sample Input

2 4


Sample Output

5


Solution

1 表示当前位置放一个向下的砖,用 0 表示当前位置没有放一个向下的砖(可能是一个向右的砖,也有可能是一个向左的砖),然后把一个 01 串压缩成十进制,表示一行的状态。
预处理两个状态之间的关系,即上下行之间的关系。然后用DP,统计可以达到当前行这样状态的数量。


Code

#include <iostream>#include <cstdio>#include <cstring>#define LL long longusing namespace std;LL cnt,w,h,MAXN,ans,oo;bool can[5000][5000];LL num[5000];LL head[5000];LL nxt[100000];LL data[100000];LL f[5000][20];void add(LL x,LL y){    nxt[cnt]=head[x];data[cnt]=y;head[x]=cnt++;}void dfs(LL pre,LL step,LL now){    if(step==h){        if(!can[now][pre]){            can[now][pre]=true;            add(now,pre);        }        return;    }    if(pre&(1<<step)){        dfs(pre,step+1,now);    }    else{        dfs(pre,step+1,now|(1<<step));        if(!(pre&(1<<(step+1)))&&step<h-1)dfs(pre,step+2,now);    }}LL dp(LL x,LL y){    LL sum=0;    if(f[x][y]!=oo)return f[x][y];    if(y==0)return x==0?1:0;    for(LL i=head[x];i!=-1;i=nxt[i]){        LL tmp=dp(data[i],y-1);        sum+=tmp;    }    return f[x][y]=sum;}int main(){    freopen("floor.in","r",stdin);    freopen("floor.out","w",stdout);    memset(f,0x3f,sizeof f);oo=f[0][0];    memset(head,-1,sizeof head);    scanf("%lld%lld",&w,&h);MAXN=(1<<(h));    for(LL i=0;i<MAXN;i++)        dfs(i,0,0);    LL tmp=dp(0,w);    ans+=tmp;    printf("%lld\n",ans);    return 0;}
0 0