"SCOI2005" non-aggression (like pressure DP)

Topic Link
in (N \ times N \) \ board put inside \ (K \) king, so 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 \ (8 \) squares.
\ (. 1 \ Le N \ Le 9,0 \ Le K \ Le N * N \)
\ (F (I, J, L) \) prior to represent \ (I \) line, the current state is \ (J \ ) , and has been placed \ (l \) scheme when kings.
\ (j \) This dimension is represented by a binary
first pre-processing on one line all legal status (ie excluding the case of two adjacent on the same line), then direct enumeration state to match those on the line can be.
\ (F (I, J, L) = \ SUM F (I-1, x, L-NUM (x)) \)
\ (NUM (x) \) is x the number of 1 in a binary
when transferring to exclusion between two lines of the king to attack each other unlawful situation.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> sta,stan;
ll d[10][(1<<10)][100];
int n,k;
bool ok(int i,int j){
    if(i & j)return false;
    if((i << 1) & j)return false;
    if(i & (j << 1))return false;
    return true;
}
int main(){
    scanf("%d%d",&n,&k);
    for(int i=0;i<(1<<n);i++){
        int num = 0;
        bool flag = true;
        for(int j=0;j<n-1;j++){
            if(i >> j & 1){
                num++;
                if(i >> (j+1) & 1){
                    flag = false;
                    break;
                }
            }
        }
        if(!flag)continue;
        sta.push_back(i);
        stan.push_back(num + (i >> (n-1) & 1));
    }
    for(int i=0;i<sta.size();i++){
        d[1][i][stan[i]] = 1;
    }
    for(int i=2;i<=n;i++){
        for(int j=0;j<sta.size();j++){
            for(int t=0;t<sta.size();t++){
                if(ok(sta[j],sta[t])){
                    for(int p = stan[j];p <= k;p++){
                        d[i][j][p] += d[i-1][t][p-stan[j]];
                    }
                }
            }
        }
    }
    ll res = 0;
    for(int i=0;i<sta.size();i++)
        res += d[n][i][k];
    cout<<res<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/1625--H/p/11268069.html