1087: [SCOI2005]互不侵犯King

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1499  Solved: 872
[Submit][Status]

Description

在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案。国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子。

Input

只有一行,包含两个数N,K ( 1 <=N <=9, 0 <= K <= N * N)

Output

方案数。

Sample Input

3 2

Sample Output

16

HINT

 

Source

题解:

终于A了这道题。。。

其实意识到对安放的国王总个数有限制,那就应该是DP了

又因为是在棋盘上,所以以每行为状态进行转移

又因为n很小,所以状压DP。。。

主要是做一些预处理

代码:

 var  tot,n,i,j1,j2,j,k,l:longint;
s:ansistring;
ans:int64;
a:array[..,..] of boolean;
f:array[..,..,..] of longint;
can:array[..] of boolean;
calc:array[..] of longint;
function check(x,y:longint):boolean;
var s1,s2:ansistring;
i:longint;
begin
s1:=binstr(x,n);
s2:=binstr(y,n);
for i:= to n do
if (s1[i]='') and ((s2[i-]='') or (s2[i]='') or (s2[i+]='')) then exit(false);
exit(true);
end;
function cann(x:longint):boolean;
var s:ansistring;
i:longint;
begin
s:=binstr(x,n);
for i:= to n do if (s[i]='') and (s[i]=s[i-]) then exit(false);
exit(true);
end; procedure init;
begin
readln(n,k);
tot:=<<n-;
for i:= to tot do
begin
calc[i]:=;
s:=binstr(i,n);
for j:= to n do if s[j]='' then inc(calc[i]);
end;
fillchar(f,sizeof(f),);
for i:= to tot do f[,i,calc[i]]:=;
for i:= to tot do
if cann(i) then can[i]:=true;
for i:= to tot do
if can[i] then
for j:= to tot do
if can[j] then
if check(i,j) then a[i,j]:=true;
//for i:= to tot do writeln(can[i],' ',calc[i]);
end;
procedure main;
begin
for i:= to n do
for j1:= to tot do
if can[j1] then
for l:=calc[j1] to k do
for j2:= to tot do
if (can[j2]) and (a[j1,j2]) then
inc(f[i,j1,l],f[i-,j2,l-calc[j1]]);
// for i:= to n do
// for j:= to tot do
// for l:= to n do writeln(f[i,j,l]);
ans:=;
for i:= to tot do if can[i] then inc(ans,f[n,i,k]);
writeln(ans);
end;
begin
assign(input,'input.txt');assign(output,'output.txt');
reset(input);rewrite(output);
init;
main;
close(input);close(output);
end.

SCOI2005互不侵犯King的更多相关文章

  1. BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3336  Solved: 1936[Submit][ ...

  2. 洛谷1377 M国王 (SCOI2005互不侵犯King)

    洛谷1377 M国王 (SCOI2005互不侵犯King) 本题地址:http://www.luogu.org/problem/show?pid=1377 题目描述 天天都是n皇后,多么无聊啊.我们来 ...

  3. 1087: [SCOI2005]互不侵犯King

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4276  Solved: 2471[Submit][ ...

  4. BZOJ1087 SCOI2005 互不侵犯King 【状压DP】

    BZOJ1087 SCOI2005 互不侵犯King Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附 ...

  5. bzoj 1087 [SCOI2005]互不侵犯King 状态压缩dp

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Descripti ...

  6. 洛谷P1896 [SCOI2005]互不侵犯King

    P1896 [SCOI2005]互不侵犯King 题目描述 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共 ...

  7. BZOJ 1087:[SCOI2005]互不侵犯King(状压DP)

    [SCOI2005]互不侵犯King [题目描述] 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子 ...

  8. [Bzoj1083][SCOI2005]互不侵犯king(状压dp)

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4595  Solved: 2664[Submit][ ...

  9. [bzoj1087][scoi2005]互不侵犯king

    题目大意 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上 左下右上右下八个方向上附近的各一个格子,共8个格子. 思路 首先,搜索可以放弃,因为这是一 ...

随机推荐

  1. JSP九大内置对象(转载)

    JSP中一共预先定义了9个这样的对象,分别为:request.response.session.application.out.pagecontext.config.page.exception 1. ...

  2. c&c++函数的参数和返回值的传递终结版

    c++函数的参数和返回值的传递方式有三种:值传递.指针传递和引用传递. 在这之前先看几个例子: 一, int a=10; int b=a; b+=10; 此时b是a的一个拷贝,改变b的值,a并不会受到 ...

  3. C#生成二维码名片

    摘自<31天学会CRM项目开发<C#编程入门级项目实战>> 本例将使用ThoughtWorks.QRCode类库生成二维码名片.正式编码前,先了解一下什么是vCard?它是一种 ...

  4. Winform窗口弹出位置控制

    窗体的弹出位置可以由属性StartPosition来指定,默认值有: Manural 自定义,由属性Location指定: CenterScreen 屏幕中央: WindowsDefaultBound ...

  5. memcached完全剖析--1

    memcached的基础 翻译一篇技术评论社的文章,是讲memcached的连载.fcicq同学说这个东西很有用,希望大家喜欢. 发表日:2008/7/2 作者:长野雅广(Masahiro Nagan ...

  6. MySQL中SQL语句的分类

    1:数据定义语言(DDL) :创建和删除数据库(CREATE DATABASE || DROP  DATABASE):2:创建.修改.重命名.删除表(CREATE  TABLE || ALTER TA ...

  7. Magento Api 记录

    magento api 首次接触 (-) /** * magento Api 身份验证 调用示例 * Example of simple product POST using Admin accoun ...

  8. JS-------DOM0级事件处理和DOM2级事件处理-------简单记法

    0级DOM 分为2个:一是在标签内写onclick事件  二是在JS写onlicke=function(){}函数 1) <input id="myButton" type= ...

  9. codeforces 630K - Indivisibility

    K. Indivisibility 题意:给一个n(1 <= n <= 10^18)的区间,问区间中有多少个数不能被2~10这些数整除: 整除只需要看素数即可,只有2,3,5,7四个素数: ...

  10. PL/SQL — 集合及常用方法

    PL/SQL中提供了常用的三种集合联合数组.嵌套表.变长数组,而对于这几个集合类型中元素的操作,PL/SQL提供了相应的函数或过程来操纵数组中的元素或下标.这些函数或过程称为集合方法.一个集合方法就是 ...