题意:用1*2的木板,去覆盖矩阵,求覆盖方法数。
这题是看人家的题解写的,状态的表示有点不一样,横着覆盖就表示成11,竖着的就用上一行为0,下一行为1来表示,这题的关键在于位运算,|和&的用法,特别是判断两行是否和法,除了两行|后要全为1,还要注意,除了竖放的其他木块要合法,刚开始这里错了,simple过不了,后来看人家的代码发现问题。这题还有其他解法,好像有数学方法,还有插头dp,可惜我都不会T_T,有空就研究下。
Run ID | User | Problem | Result | Memory | Time | Language | Code Length | Submit Time |
9225392 | 201030720425 | 2411 | Accepted | 344K | 563MS | C++ | 944B | 2011-08-24 23:13:55 |
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
bool ac[1<<11];
int n,m,mac;
long long dp[1<<11][11];
bool check(int x)
{
int count=0;
while(x)
{
if(x&1)count++;
else
{
if(count&1)
return false;
else count=0;
}
x=x>>1;
}
if(count&1)return false;
return true;
}
bool AC(int x,int y)
{
if((x|y)!=(1<<n)-1)
return false;
else
return ac[x&y];
}
void DP()
{
memset(dp,0,sizeof(dp));
for(int i=0;i<(1<<n);i++)
if(check(i))
ac[i]=dp[i][0]=1;
for(int i=1;i<m;i++)
for(int j=0;j<(1<<n);j++)
for(int k=0;k<(1<<n);k++)
{
if(!AC(j,k)) continue;
//if(dp[k][i-1]==0) continue;
dp[j][i]+=dp[k][i-1];
}
printf("%lld\n",dp[(1<<n)-1][m-1]);
}
int main()
{
while(1)
{
scanf("%d%d",&m,&n);
if(m==0&&n==0)return 0;
DP();
}
return 0;
}