小国王

小国王

在这里插入图片描述

具体代码如下

#include<iostream>
#include<cstring>
#include<vector>

using namespace std;

typedef long long LL;

const int N = 12, M = 1 << 10, K = 110;

int n, m, cnt[M];
vector<int> state;
vector<int> head[M];
LL f[N][K][M];

bool check(int state){
    
    if((state << 1) & state || (state >> 1) & state) return false;
    return true;
}

int count(int state){
    
    int res = 0;
    for(int i = 0; i < n; ++ i) res += state >> i & 1;
    return res;
}

int main(){
    
    cin >> n >> m;
    
    //预处理出所有合法的二进制数
    for(int i = 0; i < 1 << n; ++ i)
        if(check(i)){
            state.push_back(i);
            cnt[i] = count(i);
        }
    
    //预处理出可以与一个状态联系的所有状态  
    for(int i = 0; i < state.size(); ++ i)
        for(int j = 0; j < state.size(); ++ j){
            
            int a = state[i], b = state[j];
            
            if((a & b) == 0 && check(a | b))
                head[i].push_back(j);
        }

        
   f[0][0][0] = 1;
    
    for(int i = 1; i <= n + 1; ++ i)
        for(int j = 0; j <= m; ++ j)
            for(int a = 0; a < state.size(); ++ a)
                for(auto b : head[a]){
                    
                    int c = cnt[state[a]];
                    if(j >= c)
                        f[i][j][a] += f[i - 1][j - c][b];
                }

    //第n + 1行没有放置国王的方案数就是前n行放置了m个国王的所有合法方案数      
    cout << f[n + 1][m][0] << endl;
    
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值