Questions surface: https://www.luogu.org/problemnew/show/P1896
Effect: put the King in the K N × N chessboard, that they do not attack each other, how many programs there are placed. The king can attack it down to the left and right, upper left and lower left eight directions on the upper right lower right of each grid in the vicinity of a total of eight grid.
Shaped pressure DP
It represents the current state of the binary.
1 means yes, 0 for no.
Then the question f [i] [j] [w] denotes the i-th row, the state is j, with a king w.
See equation code.
Bitset which is a binary digit.
a.count () represents the number of binary numbers 1.
VIS [] array is qualified line state.
code show as below:
#include<bits/stdc++.h> #define int long long #define rr register using namespace std; int n,m,ans=0; bool vis[1<<9]; int f[10][1<<9][90],g[1<<9]; signed main() { scanf("%lld%lld",&n,&m); for(rr int i=0;i<(1<<n);i++){ if(!(i&(i<<1)) && !(i&(i>>1))) vis[i]=1; bitset <9> a(i); g[i]=a.count(); if(vis[i]) f[1][i][g[i]]=1; } for(rr int i=2;i<=n;i++) for(rr int j=0;j<(1<<n);j++) if(vis[j]) for(rr int k=0;k<(1<<n);k++)///? if(vis[k] && !(j&(k<<1)) && !(j&(k>>1)) && !(j&k)) for(rr int w=0;w+g[k]<=m;w++) f[i][k][w+g[k]]+=f[i-1][j][w]; for(rr int i=0;i<(1<<n);i++) ans+=f[n][i][m]; printf("%lld\n",ans); // system("pause"); return 0; }
Because the data strengthen, remember to open long long.