BZOJ 1087:[SCOI2005]互不侵犯King(状压DP)
[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)的更多相关文章
- BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3336 Solved: 1936[Submit][ ...
- BZOJ 1087 [SCOI2005]互不侵犯King ——状压DP
[题目分析] 沉迷水题,吃枣药丸. [代码] #include <cstdio> #include <cstring> #include <iostream> #i ...
- bzoj 1087 [SCOI2005]互不侵犯King 状态压缩dp
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Descripti ...
- 【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 ...
- [BZOJ1087] [SCOI2005] 互不侵犯King (状压dp)
Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包 ...
- 互不侵犯king (状压dp)
互不侵犯king (状压dp) 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子.\(1\le n\ ...
- BZOJ-1087 互不侵犯King 状压DP+DFS预处理
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2337 Solved: 1366 [Submit][ ...
- bzoj1087 互不侵犯King 状压dp+bitset
题目传送门 题目大意:中文题面. 思路:又是格子,n又只有9,所以肯定是状压dp,很明显上面一行的摆放位置会影响下一行,所以先预处理出怎样的二进制摆放法可以放在上下相邻的两行,这里推荐使用bitset ...
- BZOJ 1087 [SCOI2005]互不侵犯King(状压DP)
题意:在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子.n<=9 思路:状压dp,dp[i][ ...
- [SCOI2005]互不侵犯(状压DP)
嗝~算是状压DP的经典题了~ #\(\mathcal{\color{red}{Description}}\) 在\(N×N\)的棋盘里面放\(K\)个国王,使他们互不攻击,共有多少种摆放方案.国王能攻 ...
随机推荐
- slenium的xpath几种定位方式
练习地址,以下面地址为例: http://www.w3school.com.cn/example/xmle/books.xml 1. 查找book对象 //book #所有的数 //book[1] ...
- 【BZOJ1040】[ZJOI2008] 骑士(基环外向树DP)
点此看题面 大致题意: 给你一片基环外向树森林,如果选定了一个点,就不能选择与其相邻的节点.求选中点的最大权值和. 树形\(DP\) 此题应该是 树形\(DP\) 的一个升级版:基环外向树\(DP\) ...
- ubuntu or centos 网卡无法启动
[root@seasoned-bro:/home/daeh0f]# /etc/init.d/network restart Restarting network (via systemctl): Jo ...
- Java自带工具包StringUtils包含方法
//导入包 import org.apache.commons.lang3.StringUtils //判断不为空 不包含空格 StringUtils.isNotEmpty(" " ...
- java算法面试题:写一个Singleton出来
package com.swift; public class Singleton { public static void main(String[] args) { /* * 写一个Singlet ...
- java中char类型转换成int类型的两种方法
方法一: char ch = '9'; if (Character.isDigit(ch)){ // 判断是否是数字 int num = Integer.parseInt(String.valueOf ...
- SPOJ2713GSS4 - Can you answer these queries IV(线段树)
题意 Sol 讲过无数次了..很显然,一个$10^12$的数开方不超过$8$次后就会变为$1$ 因此直接暴力更改即可,维护一下这段区间是否被全改为了$1$ 双倍经验:https://www.luogu ...
- C/C++程序基础 (八)数据结构
非递归先序遍历 // 输出, 遍历左子树,遍历右子树 void firstOrder(Node* root) { stack<Node*> leftNodes; Node* curr = ...
- jstl(c)标签
一.EL表达式: Expression Language提供了在 JSP 脚本编制元素范围外(例如:脚本标签)使用运行时表达式的功能.脚本编制元素是指页面中能够用于在JSP 文件中嵌入 Java 代码 ...
- h5获取摄像头拍照功能
完整代码展示 <!DOCTYPE html> <head> <title>HTML5 GetUserMedia Demo</title> <met ...