[Bzoj1087] [SCOI2005] non-aggression King (like pressure dp)

Topic links: https://www.lydsy.com/JudgeOnline/problem.php?id=1087

Good compression state title, the King's position as one and the other as 0, n line state may appear in each row is 1 << n, n if taking into account the state line on the 1 << ( n * n), it will be impossible.

Therefore, first of all pre-valid status of each line, and does not appear adjacent to 1, and the number of processing each state 1.

Defined state DP [i] [j] [k] 1-i denotes the i-th row j state behavior, 1-i has a number of rows of the k king scheme.

Proceeds to dp [i] [j] [k] = \ sum dp [i-1] [{j} '] [{k}'], {j} 'and {k}' is a legal state can be transferred to .

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 ll dp[15][1100][110];
 5 ll st[1100], kth[1100];
 6 int main() {
 7     int n, k;
 8     scanf("%d%d", &n, &k);
 9         int len = 0;
10         for (int i = 0; i < (1 << n); i++) {
11             if (!((i << 1)&i)) {
12                 st[++len] = i;
13                 kth[len] = 0;
14                 int w = i;
15                 while (w) {
16                     kth[len] += w % 2;
17                     w /= 2;
18                 }
19             }
20         }
21         for (int i = 1; i <= len; i++) {
22             if (kth[i] <= k)
23                 dp[1][i][kth[i]] = 1;
24         }
25         for (int i = 2; i <= n; i++) {
26             for (int j = 1; j <= len; j++) {
27                 for (int p = 1; p <= len; p++) {
28                     if ((st[j] & st[p]))
29                         continue;
30                     if ((st[j] & (st[p] << 1)))
31                         continue;
32                     if (((st[j] << 1) & st[p]))
33                         continue;
34                     for (int kk = 1; kk <= k; kk++) {
35                         if (kth[j] + kk <= k) {
36                             dp[i][j][kth[j] + kk] += dp[i - 1][p][kk];
37                         }
38                     }
39 
40                 }
41             }
42         }
43         ll ans = 0;
44         for (int i = 1; i <= n; i++) {
45             for (int j = 1; j <= len; j++) {
46                 ans += dp[i][j][k];
47             }
48         }
49         printf("%lld\n", ans);
50         return 0;
51 }

 

Guess you like

Origin www.cnblogs.com/sainsist/p/11118196.html