Original question:
223. Little Kings
Time limit per test:0.5 sec.
Memory limit per test:65536 KB
Input:standard
Output:standard
After solving nice problems on bishops and rooks, Petya decided that he would like to learn to play chess. He started to learn, the rules and found out, the most important piece in the game is the king.
The king can move to any adjacent cell (there is up to eight such cells). Thus, Kings is in the attacking position, if they is located on the adjacent cells.
Of course, the first thing Petya wants to know are the number of ways one can position K kings on a chessboard of size NX n So and No. them is in the attacking position. Help him!
Input
The input file contains the integers n (1≤n≤10) and K (0≤K≤N2).
Output
Print a line containing the total number of ways one can put the given number of kings on a chessboard of the given size s o that no. them is in attacking positions.
Sample Test (s)
Input
Test #1
3 2
Test #2
4 4
Output
Test #1
16
Test #2
79
Effect:
I'll give you a chessboard of size nxn, with K kings on it. Each king's attack range is 8 of a circle around. Now ask you the size of the NXN chess board put K King how many kinds of placement method.
#include <bits/stdc++.h> #include <iostream> #include <cstring> using namespace std;
FStream in,out;
Long Long dp[11][1025][101];
int ONE[1025];//1 number int state[1025];//state number int n,k,index;
int totalone (int x)//calculates how many 1 {int coun=0 are in a binary number;
while (x) {x=x& (x-1);
coun++;
} return Coun;
} bool linecmp (int i)//Determines whether a state (that is, a row on the board) satisfies the requirements {for (int j=1,k=1;j<= (1<<n); j= (1<<k), k++) { if (j&i) {if (j<<1) &i| |
(j>>1) &i) return false;
}} return true;
} void Init ()//Initialize {memset (dp,0,sizeof (DP));
memset (One,0,sizeof (one));
memset (state,0,sizeof (state));
index=0;
Dp[0][1][0]=1;
for (int i=0;i<= (1<<n) -1;i++)//Find out all the states that a row can appear in, save in state//And how many 1 of that status are saved in one {int tot=totalone (i);
if (linecmp (i) &&tot<=k&&tot<= (n+1)/2) {++index; One[indeX]=totalone (i);//state[index]=i; }}} bool twolinecmp (int x,int y)//Determine if the above line and the following line conflict {if (x&y| | (y& (x>>1)) | |
(y& (x<<1)))
return false;
return true;
} int main () {Ios::sync_with_stdio (false); while (cin>>n>>k) {init ();/* for (int i=1;i<=index;++i) cout<<bitset< 10> (State[i]) << "" "<<one[i]<<endl;*/for (int. i=1;i<=n;++i) {for (int j= 1;J<=INDEX;++J) {for (int. Kk=1;kk<=index;++kk) {for
(int l=0;l<=k;++l) {if (l-one[j]>=0&&twolinecmp (State[j],state[kk])) dp[i][j][
L]+=DP[I-1][KK][L-ONE[J]];
}}}} long long ans=0;
for (int i=1;i<=index;++i) ans+=dp[n][i][k]; cout<<ans<<endl;
} return 0;
}
Answer: The topic on the SGU that is recommended on the
Tutorial is also the topic I submitted on the first SGU.
Just contact state compression, transfer equation is very good to think, Dp[i][x][k]+=dp[i-1][y][k-one (x)]. The
indicates that line I is using the K-King placement method under state x equals all the sum of the number of kings minus the state x used in the I-1 line State Y. Both the state and the number are stored in an array.
My code is pretty rotten, and I'm not going to be able to enumerate all States recursively. Finally, the answer is to add all the rows to N and the king's State of K.