状态压缩DP---------------骑士

在 n×nn×n 的棋盘上放 kk 个国王,国王可攻击相邻的 88 个格子,求使它们无法互相攻击的方案总数。
输入格式
共一行,包含两个整数 nn 和 kk。
输出格式
共一行,表示方案总数,若不能够放置则输出00。
数据范围
1≤n≤101≤n≤10,

0≤k≤n20≤k≤n2
输入样例:
3 2

输出样例:
16

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 12, M = 1 << 10, k = 110;
typedef  long long LL;
LL f[N][k][M];
int cnt[M];
vector<int> state;
vector<int> head[M];
int n, m;
bool check(int x){
 for (int i = 0; i < n; i ++)
 if ((x >> i & 1) && (x >> i + 1 & 1))
 return false;
 return true;
}
int count(int x){
 int res = 0;
 for (int i = 0; i < n; i ++)   res += x >> 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 (int b : head[a]){
             int c = cnt[state[a]];
             if (j >= c){
              f[i][j][a] += f[i - 1][j - c][b];
       }
     }
     cout << f[n + 1][m][0] << endl;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值