P1869 non-aggression (like a simple pressure DP)

The meaning of problems: 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 the surrounding eight grid.

 

Solution: First, only consider a line of legal pendulum method, not only to ensure the election of an adjacent grid line.

1 represents the i-th row in the column can be stored together with the legal status of a string of binary numbers, each bit i of the number of placement king

For example, 101001 is legal, it is illegal 101101

Preprocessing each of a legal state, the number of the grid is selected sta (x)

Set F (i, j, l) denotes the i-th row has been processed, the state of the Bank j, l in total have chosen a total number of programs

State is provided on line x, if x and j can be obtained without contradiction state transition equation

f (i, j, l) = Σf (i-1, x, the sta (x))

 The answer is Σf (i, n, k)

 

 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 long long sta[2005], sit[2005], f[15][2005][105];
 6 
 7 int n, k, cnt;
 8 
 9 void dfs(int x,int num,int cur){
10 
11   if(cur>=n){
12 
13       sit[++cnt]=x;
14 
15       sta[cnt]=num;
16 
17       return;
18 
19   }
20 
21   dfs(x,num,cur+1);
22 
23   dfs(x+(1<<cur),num+1,cur+2);
24 
25 }
26 
27 int main(){
28 
29   cin>>n>>k;
30 
31   dfs(0,0,0);
32 
33   for(int i=1;i<=cnt;++i)f[1][i][sta[i]]=1;
34 
35   for(int i=1;i<=n;++i)
36 
37     for(int j=1;j<=cnt;++j)
38 
39       for(int l=1;l<=cnt;++l){
40 
41           if (sit[j] & sit[l]) continue;
42 
43         if ((sit[j] << 1) & sit[l]) continue;
44 
45         if (sit[j] & (sit[l] << 1)) continue;
46 
47         for(int p=sta[j];p<=k;++p)
48 
49           f[i][j][p]+=f[i-1][l][p-sta[j]];
50 
51          }
52 
53   long long ans = 0;
54 
55   for(int i=1;i<=cnt;++i)
56 
57     ans+=f[n][i][k];
58 
59   cout<<ans<<endl;
60 
61 }
View Code

 

Guess you like

Origin www.cnblogs.com/vv123/p/12319782.html