[SCOI2005]互不侵犯King

【题目描述】

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

【输入】

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

【输出】

方案数。

【样例输入】

3 2

【样例输出】

16

分析:

经典的状压DP题目,可我竟然调了很长时间都没对,后来发现是DP枚举范围错了,直接枚举到最大情况导致答案大得离谱,所以透彻理解算法很重要。

其它懒得写了。

代码1(第一次写的代码很丑,还很多余地在每个二进制首位增添一个1......):

program king;
var
f:array[..,..,..]of int64;
a:array[..,..]of ..;
sum1,w:array[..]of longint;
g:array[..,..]of boolean;
h:array[..]of boolean;
n,i,m,j,k,u,v,l,num,num1:longint;
procedure work(x:longint);
var i,c,y:longint;
begin
i:=x; c:=;
while i> do
begin
a[x,c]:=i mod ; if a[x,c]= then begin inc(sum1[x]); if c> then y:=-c+; end;
i:=i div ;
c:=c-;
end;
dec(sum1[x]); w[x]:=y;
end;
begin
readln(n,k);num1:=;num:=;
for i:= to n do num:=num*;
num:=num1+num-;
for i:=num1 to num do
begin
work(i);
end;
for u:=num1 to num do
for v:=num1 to num do
for l:= to do begin g[u,v]:=true;
if (a[u,l]=)and(((a[v,l-]=)and(l>))or(a[v,l]=)or(a[v,l+]=)) then begin g[u,v]:=false;break; end;
if (a[v,l]=)and(((a[u,l-]=)and(l>))or(a[u,l]=)or(a[u,l+]=)) then begin g[u,v]:=false;break; end;
end;
fillchar(f,sizeof(f),);
for i:=num1 to num do begin h[i]:=true;
for j:= to do
if ((a[i,j]=)and(((a[i,j-]=)and(j>))or(a[i,j+]=))) then begin h[i]:=false; break; end;
if h[i]=true then f[,i,sum1[i]]:=;
end;
for i:= to n+ do
for j:= to k do
for u:=num1 to num do
if (sum1[u]<=j)and(h[u]=true) then
for v:=num1 to num do
if (sum1[v]+sum1[u]<=j)and(h[v]=true) then
begin
if g[u,v]=true then f[i,u,j]:=f[i,u,j]+f[i-,v,j-sum1[u]];
end;
writeln(f[n+,num1,k]);
end.

代码2(改进的代码,用了一些位运算,比第一次好看一点):

program king;
var
f:array[..,..,..]of int64;
sum1:array[..]of longint;
g:array[..,..]of boolean;
h:array[..]of boolean;
n,i,m,j,k,u,v,l,t,num:longint;
begin
readln(n,k);
fillchar(g,sizeof(g),false);
fillchar(h,sizeof(h),false);
fillchar(f,sizeof(f),);
num:=;
for i:= to n do num:=num*; num:=num-;
for i:= to num do
if i and (i shr )= then
begin
j:=i;t:=;
while j> do begin t:=t+j and ; j:=j shr ; end;
sum1[i]:=t; h[i]:=true;
end;
for i:= to num do if h[i]=true then
for j:= to num do if h[j]=true then
if (i and j=)and(i and (j shr )=)and(j and (i shr )=) then g[i,j]:=true;
for i:= to num do if h[i]=true then f[,i,sum1[i]]:=;
for i:= to n+ do
for j:= to k do
for u:= to num do if (h[u]=true)and(sum1[u]<=j) then
for v:= to num do if (h[v]=true)and(sum1[u]+sum1[v]<=j) then
if g[v,u]=true then
inc(f[i,u,j],f[i-,v,j-sum1[u]]);
writeln(f[n+,,k]);
end.

BZOJ 1087:[SCOI2005]互不侵犯King(状压DP)的更多相关文章

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

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

  2. BZOJ 1087 [SCOI2005]互不侵犯King ——状压DP

    [题目分析] 沉迷水题,吃枣药丸. [代码] #include <cstdio> #include <cstring> #include <iostream> #i ...

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

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

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

    经典状压DP. f[i][j][k]=sum(f[i-1][j-cnt[k]][k]); cnt[i]放置情况为i时的国王数量 前I行放置情况为k时国王数量为J #include <iostre ...

  5. [BZOJ1087] [SCOI2005] 互不侵犯King (状压dp)

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

  6. 互不侵犯king (状压dp)

    互不侵犯king (状压dp) 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子.\(1\le n\ ...

  7. BZOJ-1087 互不侵犯King 状压DP+DFS预处理

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2337 Solved: 1366 [Submit][ ...

  8. bzoj1087 互不侵犯King 状压dp+bitset

    题目传送门 题目大意:中文题面. 思路:又是格子,n又只有9,所以肯定是状压dp,很明显上面一行的摆放位置会影响下一行,所以先预处理出怎样的二进制摆放法可以放在上下相邻的两行,这里推荐使用bitset ...

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

    题意:在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子.n<=9 思路:状压dp,dp[i][ ...

  10. [SCOI2005]互不侵犯(状压DP)

    嗝~算是状压DP的经典题了~ #\(\mathcal{\color{red}{Description}}\) 在\(N×N\)的棋盘里面放\(K\)个国王,使他们互不攻击,共有多少种摆放方案.国王能攻 ...

随机推荐

  1. file - 确定文件类型

    总览 file [ -bcnsvzL ] [ -f 命名文件 ] [ -m 幻数文件 ] file ... 描述 本手册页说明了3.27版本 file 命令的使用. File 命令试图检查每个参数以判 ...

  2. 深入理解计算机系统_3e 第九章家庭作业 CS:APP3e chapter 9 homework

    9.11 A. 00001001 111100 B. +----------------------------+ | Parameter Value | +--------------------- ...

  3. 支持向量机: Maximum Margin Classifier

    支持向量机即 Support Vector Machine,简称 SVM .我最开始听说这头机器的名号的时候,一种神秘感就油然而生,似乎把 Support 这么一个具体的动作和 Vector 这么一个 ...

  4. systemd 中的requires, wants, before, after

    man systemd.unit    man systemd.service ###依赖关系和前后顺序* 依赖关系:Requires和Wants * 前后顺序:After,Before 依赖关系,前 ...

  5. Linux---cp命令学习

    cp命令 cp source_file  target_file 能够复制文件,如果target_file所指定的文件不存在,cp就创建这个文件,如果已经存在,就把文件内容清空并把source_fil ...

  6. BundleConfig的作用

    在ASP.NET MVC4中(在WebForm中应该也有),有一个叫做Bundle的东西,它用来将js和css进行压缩(多个文件可以打包成一个文件),并且可以区分调试和非调试,在调试时不进行压缩,以原 ...

  7. vue2.0父子组件以及非父子组件通信

    官网API: https://cn.vuejs.org/v2/guide/components.html#Prop 一.父子组件通信 1.父组件传递数据给子组件,使用props属性来实现 传递普通字符 ...

  8. SpringBoot之YAML

    SpringBoot的配置文件有两种,一种是properties结尾的,一种是以yaml或yml文件结尾的 我们讨论一下yml文件结尾的文件: 基本语法: 其实yml文件就是键值对的形式,不过就是键( ...

  9. JAVA 优先获取外网Ip,再获取内网Ip

    1.获取内网Ip private String getLocalhostIp(){ String hostAddress = ""; try { InetAddress addre ...

  10. 解决Linux使用php命令 -base comment not found并安装composer

    获取php的安装目录 使用 find / -name php.ini 查看php的安装位置 /usr/local/php/lib/php.ini # cd 到/usr/local/php/lib/ph ...