【分析】
每一行是否合法只和上一行有关,所以可以状压dp,dp[i][j][k]表示第i行,一共放了j个,当前行状态为k的方案数。
【代码】
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#define N 50005
#define M 200004
#define INF 1e9+1
#define mod 100000000
using namespace std;
typedef long long ll;
int read()
{
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
int n,m,top;
ll ans;
ll dp[10][100][1050];
int st[1050],num[1050];
bool Judge(int x) {
return x&(x<<1);
}
bool Check(int j,int k) {
return (st[j]&st[k])||(st[k]&(st[j]>>1))||(st[j]&(st[k]>>1));
}
int Count(int x)
{
int rtn=0;
for(int i=0;i<n;i++) if((1<<i)&x) rtn++;
return rtn;
}
int main()
{
n=read(),m=read();
for(int i=0;i<1<<n;i++)
if(!Judge(i)) st[++top]=i,num[top]=Count(i),dp[1][num[top]][top]=1;
for(int i=2;i<=n;i++)
for(int j=1;j<=top;j++)
{
for(int l=num[j];l<=m;l++) if(dp[i-1][l][j])
for(int k=1;k<=top;k++) if(!Check(j,k)&&l+num[k]<=m)
dp[i][l+num[k]][k]+=dp[i-1][l][j];
}
for(int i=1;i<=top;i++) ans+=dp[n][m][i];
printf("%lld\n",ans);
return 0;
}