king

https://loj.ac/problem/10170

Title Description

  In (n \ times n \) \ put on the board \ (k \) king, the king can attack adjacent \ (8 \) total program grid, demand that they can not attack each other.

Thinking

  This question of \ (n \) is relatively small, we consider directly to the board of his party pressed into a number, then put a piece if this position, the lower the number the binary number is \ (1 \) , otherwise \ ( 0 \) , we can first of all pre-treatment at several rows satisfy the condition, the (DP \) \ columns, with \ (f [i] [S ] [k] \) represents \ (I \) line state as \ (S \) , until now were put \ (k \) number of pieces of the program, then \ (S \) can never attack each other over the state transition directly \ (dp \) can be.

Code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int s[600],p[600];
ll f[11][500][110];
int main()
{
    int n,k,cnt=0;
    scanf("%d%d",&n,&k);
    for(int i=0;i<(1<<n);i++)
    {
        if(i&(i<<1))continue ;
        int sum=0;
        for(int j=0;j<n;j++)
            if(i&(1<<j))sum++;
        p[++cnt]=i;
        s[cnt]=sum;
    }
    f[0][1][0]=1;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=cnt;j++)
            for(int l=s[j];l<=k;l++)
            {
                for(int r=1;r<=cnt;r++)
                    if(!(p[j]&p[r])&&!(p[j]&(p[r]<<1))&&!(p[j]&(p[r]>>1)))
                        f[i][j][l]+=f[i-1][r][l-s[j]];
            }
    long long ans=0;
    for(int i=1;i<=cnt;i++)
        ans+=f[n][i][k];
    printf("%lld\n",ans);
}

Guess you like

Origin www.cnblogs.com/fangbozhen/p/11844454.html