Time Limit:10 Sec Memory limit:162 MB
submit:2358 solved:1380
[Submit] [Status] [Discuss] Description
Put K Kings in NxN's chessboard, so that they do not attack each other, there are many kinds of layout plan. The king can attack it up and down, as well as the left upper left lower right up to the bottom right in the next eight directions in the vicinity of a grid, a total of 8 squares.
Input
Only one row, containing two numbers n,k (1 <=n <=9, 0 <= K <= n * N)
Output
Number of scenarios.
Sample Input3 2Sample Output -
The following:
the characteristic of the pressure DP is still very obvious, the king of each row is expressed as 1, do not put the king as 0, thus can get a 01 string, this 01 string corresponding to the decimal number is compressed state. It is clear that the case for each row is determined only by the previous row, thus satisfying the no-effect of DP. F[i][k][p] denotes that line I uses case K, i-1 p,state[i] denotes the decimal number corresponding to the condition I, cost[i] represents the number of kings consumed by condition I. because of the n<=9, so for the number of cases not more than 2^9, but obviously there must be some two kings adjacent, so the bank is contradictory, how to compare with the previous line? Thus we can preprocess all cases first, put in state[] and cost[], and the state transition equation is shown in the code below.
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4 LL n,k;5 LL CNT;6LL state[1<<Ten+Ten],cost[1<<Ten+Ten];7LL f[ One][1<<Ten][ the];//F[i][j][k]//line I use case j, a total of K kings8 LL ANS;9 intMain () {Tenscanf"%d%d",&n,&K); One for(LL i=0; I<= (1<<n)-1; i++){ ALL tmp=i; - if((((i<<1) &i)) | | ((i>>1) &i))Continue; -cost[++cnt]=tmp%2; the while(Tmp= (tmp>>1)) cost[cnt]+=tmp%2; - if(cost[cnt]<=k) state[cnt]=i; - Elsecost[cnt--]=0; - } + for(LL i=1; i<=cnt;i++){ -f[1][i][cost[i]]=1; + } A for(LL i=2; i<=n;i++) {//Enumerate 2~n rows at for(LL j=1; j<=cnt;j++) {//enumerate the state of line I - for(LL p=1;p <=cnt;p++) {//enumerate the state of line I-1 - if(((state[j]<<1) &state[p]) | | ((state[j]>>1) &state[p]) | | (State[j]&state[p]))Continue;//there will be no food for each other. - for(LL k1=0; k1<=k;k1++) {//enumeration of I-1 and the number of kings previously used - if(k1+cost[j]<=k) f[i][j][k1+cost[j]]+=f[i-1][P][K1]; - } in } - } to } + for(LL i=1; i<=cnt;i++){ -ans+=F[n][i][k]; the } *cout<<ANS; $ return 0;Panax Notoginseng}
BZOJ1087:[SCOI2005] Non-aggression king