SCOI2005互不侵犯King
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
Sample Output
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的更多相关文章
- BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3336 Solved: 1936[Submit][ ...
- 洛谷1377 M国王 (SCOI2005互不侵犯King)
洛谷1377 M国王 (SCOI2005互不侵犯King) 本题地址:http://www.luogu.org/problem/show?pid=1377 题目描述 天天都是n皇后,多么无聊啊.我们来 ...
- 1087: [SCOI2005]互不侵犯King
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4276 Solved: 2471[Submit][ ...
- BZOJ1087 SCOI2005 互不侵犯King 【状压DP】
BZOJ1087 SCOI2005 互不侵犯King Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附 ...
- bzoj 1087 [SCOI2005]互不侵犯King 状态压缩dp
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Descripti ...
- 洛谷P1896 [SCOI2005]互不侵犯King
P1896 [SCOI2005]互不侵犯King 题目描述 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共 ...
- BZOJ 1087:[SCOI2005]互不侵犯King(状压DP)
[SCOI2005]互不侵犯King [题目描述] 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子 ...
- [Bzoj1083][SCOI2005]互不侵犯king(状压dp)
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4595 Solved: 2664[Submit][ ...
- [bzoj1087][scoi2005]互不侵犯king
题目大意 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上 左下右上右下八个方向上附近的各一个格子,共8个格子. 思路 首先,搜索可以放弃,因为这是一 ...
随机推荐
- Java线程间通信--生产者消费者
class ProducerConsumerDemo { public static void main(String[] args) { Resource r = new ...
- Custom Action : dynamic link library
工具:VS2010, Installshield 2008 实现功能: 创建一个C++ win32 DLL的工程,MSI 工程需要调用这个DLL,并将Basic MSI工程中的两个参数,传递给DLL, ...
- Menu bar missing from ClearCase Explorer
See following links: Menu bar missing from ClearCase Explorer Understanding the Rational ClearCase E ...
- nodejs的调试
js的调试始终是一个比较麻烦也是比较困难的事情,从最原始的alert调试,到火狐的firebug工具,在到后来各个浏览器厂商的调试工具.调试工具的发展历程,也可以看出由JS构建的业务和技术逻辑越来越复 ...
- WPF MVVM 中怎样在ViewModel总打开的对话框在窗体之前
今天在WPF的项目中,写打印插件,在ViewModel中对需要弹出打印对话框,而对话框如果没有Owner所属的时候经常会被当前应用程序遮住,导致我都不知道到底弹出来没有! 参照:http://www. ...
- rhel_6.x 安装mysql
不知为何mysql的官网很难下载,本人网上找了好久,终于找到了个镜像: 特别感谢http://mirrors.sohu.com/mysql/MySQL-5.6/ ^_^ 首先下载mysql的下面 ...
- SSH搭建完美CURD,含分页算法
今日开始研究使用java平台上的框架解决web服务端的开发. 这是一个完整的SSH实例,在马士兵老师的SSH整合代码基础上,增加用户的增删改查,同时实现structs方式的分页 放出源代码供大家学习参 ...
- Pair Project: Elevator Scheduler [电梯调度算法的实现和测试][关于电梯调度算法的附加思考]:刘耀先-11061183,罗凡-11061174
本文为对于电梯调度算法的三个附加题思考 1.改进电梯调度的interface 设计, 让它更好地反映现实, 更能让学生练习算法, 更好地实现信息隐藏和信息共享. <1>进一步提高API定义 ...
- uitableviewcell 自适应大小 参考
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { ...
- JS数组整理
1. 检测数组的方法: 1. instanceof[操作符]: var arr = []; console.log(arr instanceof Array);//true 1. instanceof ...