1.题目简介:
略。
2.思路:
看到这题的数据范围,我们很容易想到这是一道状压DP题。首先设计状态这题看似是在二维棋盘上操作,实则我们可以将数组改为一维,因为对于每一行j,我们只需以上一行作为参照判断是否合法。所以我们设表示当前摆到第行,前面已经摆了个棋子,该行的状态为。我们设表示第i行国王的放置状态为。表示状态为的国王个数。设上一行的状态为。我们就可以列出状态转移方程,对于每一个满足条件的,。然后我们就可以不断更新答案,求出最终结果了。
3.Accept Code:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int read(){//快读
int s=0,w=1;char ch=getchar();
while(ch<48||ch>57){if(ch=='-')w=-1;ch=getchar();}
while(ch>=48&&ch<=57)s=(s<<1)+(s<<3)+(ch^48),ch=getchar();
return s*w;
}
int n,k,f[15][1500][95],cnt,sit[1500],sta[1500],ans;
void dfs(int l,int num,int now){//预处理第一行的信息
if(now>=n){
sit[++cnt]=l;
sta[cnt]=num;
return ;
}
dfs(l,num,now+1);
dfs(l+(1<<now),num+1,now+2);
}
bool comf(int x,int y){//判断相邻的两行的状态是否合法
if(sit[x]&sit[y])return false;
if((sit[x]<<1)&sit[y])return false;
if(sit[x]&(sit[y]<<1))return false;
return true;
}
signed main(){
n=read(),k=read();
dfs(0,0,0);
for(int l=1;l<=cnt;l++)f[1][l][sta[l]]=1;
for(int i=2;i<=n;i++){//循环更新答案
for(int j=1;j<=cnt;j++){
for(int x=1;x<=cnt;x++){
if(!comf(j,x))continue;
for(int l=sta[j];l<=k;l++){
f[i][j][l]+=f[i-1][x][l-sta[j]];
}
}
}
}
for(int i=1;i<=cnt;i++)ans+=f[n][i][k];//统计最终答案
cout<<ans;
return 0;
}