SCOI2005互不侵犯

基础版

 

按行转移

  • 两个判断:
    • 本集合(行)内是否冲突
    • 不同行之间是否冲突(即是否互为八个方向之一)
  • 动态转移方程
    • 通过循环来实现
#include <bits/stdc++.h>
using namespace std;

const int N = 15;
int n, k;
long long f[N][N * N][1 << N];

int size(int s) 
{
int cnt = 0;
for (; s; s >>= 1) {
cnt += s & 1;
}
return cnt;
}

bool check(int s) 
{
if (s & (s << 1)) return false; //本集合内国王是否冲突 
return true;
}

bool check(int s, int t) { //两行之间的国王是否冲突 
if ((t & s) || (s & (t << 1)) || ((s << 1) & t)) return false;
return true;
}

int main() 
{ 
ios::sync_with_stdio(false);
cin >> n >> k;
int m = 1 << n;
f[0][0][0] = 1; //边界 
//f[i][j][S]表示正在处理第i行,第i行放的格子集合为S,前i行共放了j个国王 
for (int i = 1; i <= n + 1; i++) //
for (int s = 0; s < m; s++) { // 枚举当前行的状态 
if (check(s))
for (int t = 0; t < m; t++) { //枚举上一行的状态 
if (check(t) && check(s, t)) {
int sz = size(s);
for (int j = sz; j <= k; j++) //国王个数 
f[i][j][s] += f[i - 1][j - sz][t]; //由上一行转移来 
}
}
}
cout << f[n + 1][k][0]; 
}
View Code

 

(我再去研究下高级版怎么写(逃 )

posted @ 2019-10-02 12:02  QUEKI嶺冬  阅读(178)  评论(0编辑  收藏  举报
/*! Color themes for Google Code Prettify | MIT License | github.com/jmblog/color-themes-for-google-code-prettify */ .pln{color:#4d4d4c}ol.linenums{margin-top:0;margin-bottom:0;color:#8e908c}li.L0,li.L1,li.L2,li.L3,li.L4,li.L5,li.L6,li.L7,li.L8,li.L9{padding-left:1em;background-color:#fff;list-style-type:decimal!important;}@media screen{.str{color:#718c00}.kwd{color:#8959a8}.com{color:#8e908c}.typ{color:#4271ae}.lit{color:#f5871f}.pun{color:#4d4d4c}.opn{color:#4d4d4c}.clo{color:#4d4d4c}.tag{color:#c82829}.atn{color:#f5871f}.atv{color:#3e999f}.dec{color:#f5871f}.var{color:#c82829}.fun{color:#4271ae}} /*下面是我设置背景色,字体大小和字体*/ .cnblogs-markdown code{ background:#fff!important; } .cnblogs_code,.cnblogs_code span,.cnblogs-markdown .hljs{ font-size:16px!important; } .syntaxhighlighter a, .syntaxhighlighter div, .syntaxhighlighter code, .syntaxhighlighter table, .syntaxhighlighter table td, .syntaxhighlighter table tr, .syntaxhighlighter table tbody, .syntaxhighlighter table thead, .syntaxhighlighter table caption, .syntaxhighlighter textarea { font-size: 16px!important; } .cnblogs_code, .cnblogs_code span, .cnblogs-markdown .hljs{ font-family:consolas, "Source Code Pro", monaco, monospace !important; } //以上是代码高亮 /* 文字特效 */