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. Windows 7 Shortcuts (完整兼具分类有序,想我所想,赞!)

    Original Link: http://www.shortcutworld.com/en/win/Windows_7.html Table of Contents: Managing 'Windo ...

  2. WinForm实现最小化窗体时隐藏到系统托盘中

    1.首先在工具栏中选择NotifyIcon控件拖入窗体中: 2.设置NotifyIcon控件的相关属性: Icon:在系统托盘中显示的图标: Text:当鼠标移动到系统托盘图标上时显示的文本: Con ...

  3. 通过正则表达式获取url中参数

    url: http://xxxx.com?name=魅力&id=123 js中: var name = getUrlParam("name"); /*通过正则获取url中的 ...

  4. thinkphp表单上传文件并将文件路径保存到数据库中

    上传单个文件,此文以上传图片为例,上传效果如图所示 创建数据库upload_img,用于保存上传路径 CREATE TABLE `seminar_upload_img` (  `id` int(11) ...

  5. 006.Compile方法

    Delphi procedure Compile; 类型:procedure 可见性:public 所在单元:System.RegularExpressionsCore 父类:TPerlRegEx 此 ...

  6. Binding的源和路径

    书上写着:Binding的源也就是数据的源头.Binding对于源的要求很简单-只要他是一个对象!并且通过属性(Property)公开自己的数据,它就可以作为Binding的源了.就像上一篇我写的那个 ...

  7. Sybase数据库异常紧急恢复

    现象:Error 926  Severity Level 14  Error Message Text  Database 'xx' cannot be opened - it has been ma ...

  8. IBatis.net在asp.net MVC下的使用

    IBatis.net 是2001年发起的开源项目,它是一个轻量级的ORM框架,现在IBatisNET已经是属于Apache下的一个子项目了,最新版本是1.6.2. 官方网站:http://www.my ...

  9. js动态添加id

    <script type="text/javascript"> function add_id(){ var dlall=document.getElementsByT ...

  10. 上传项目到Github

    1.使用根工具(均是图形化的界面) TortoiseGit-1.8.12.0-32bit GitExtensions-2.48.05-SetupComplete 2.大致步骤 首先,你需要一个Gith ...