不知哪里有错,样例没有过,是中间的dp哪里写错了还是考虑错误?
求帮助,谢谢啦!
#include <bits/stdc++.h>
using namespace std;
int n,m;
int dp[10][82][1 << 10];
inline int sum(int x){
int cnt = 0;
while(x){
if(x & 1){
++cnt;
}
x >>= 1;
}
return cnt;
}
inline bool check(int x){
return ((x & (x >> 1)) == 0);
}
inline bool fit(int last,int now){
return (((last & now) == 0) && (((last >> 1) & now) == 0) && (((last << 1) & now) == 0));
}
int main(){
scanf("%d %d",&n,&m);
for(int i = 0;i < (1 << n);++i){
if(check(i) && sum(i) <= m){//行内不相邻
dp[1][sum(i)][i] = 1;
}
}
for(int i = 1;i <= n;++i){
for(int j = 0;j <= m;++j){//必须恰好使用m个国王
for(int k = 0;k < (1 << n);++k){
if(check(k)){
int kcnt = sum(k);
if(kcnt > k){
continue;
}
for(int last = 0;last < (1 << n);++last){
if(check(last) && fit(last,k)){
dp[i][j][k] += dp[i-1][k - kcnt][last];
}
}
}
}
}
}
int ans = 0;
for(int i = 0;i < (1 << n);++i){
ans = max(ans,dp[n][m][i]);
}
printf("%d",ans);
return 0;
}