uva11270 Tiling Dominoes

来源:互联网 发布:linux查看挂载目录 编辑:程序博客网 时间:2024/05/24 06:45

Given a rectangular grid, with dimensions m  n , compute the number
of ways of completely tiling it with dominoes. Note that if the
rotation of one tiling matches another, they still count as different
ones. A domino is a shape formed by the union of two unit squares
meeting edge-to-edge. Equivalently, it is a matching in the grid graph
formed by placing a vertex at the center of each square of the region
and connecting two vertices when they correspond to adjacent squares.
An example of a tiling is shown below. Input The input will consist of
a set of lines with mn , given the restriction n  m<
101. Output For each line of input, output the number of tilings in a separate line.

插头dp。
对于当前位置,如果上面为空只能往上放,否则的话,可以不放,如果左边为空还可以放左边。

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define LL long longLL dp[2][3010];int n,m;int main(){    int b,flag;    while (scanf("%d%d",&n,&m)==2)    {        if (n<m) swap(n,m);        memset(dp,0,sizeof(dp));        flag=(1<<m)-1;        dp[0][flag]=1;        b=0;        for (int i=0;i<n;i++)            for (int j=0;j<m;j++)            {                b^=1;                memset(dp[b],0,sizeof(dp[b]));                for (int k=0;k<=flag;k++)                {                    if (k&(1<<m-1)) dp[b][(k<<1)&flag]+=dp[b^1][k];                    if (i&&!(k&(1<<m-1))) dp[b][((k<<1)&flag)|1]+=dp[b^1][k];                    if (j&&!(k&1)&&(k&1<<m-1)) dp[b][((k<<1)&flag)|3]+=dp[b^1][k];                }            }        printf("%lld\n",dp[b][flag]);    }}
0 0
原创粉丝点击