1087. [SCOI2005]互不侵犯King【状压DP】
Description
在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案。国王能攻击到它上下左右,以及左上
左下右上右下八个方向上附近的各一个格子,共8个格子。
Input
只有一行,包含两个数N,K ( 1 <=N <=9, 0 <= K <= N * N)
Output
方案数。
Sample Input
Sample Output
f[i][j][k]表示放完i行,第i行状态为j,i行放了k个
#include<iostream>
#include<cstring>
#include<cstdio>
#define LL long long
using namespace std;
LL state[],num[];
LL f[][][];
LL N,K,m;
void Deal()
{
for (LL i=;i<=(<<N)-;++i)
{
LL x=i,pre=-,cnt=;;
for (LL j=;j<=;++j)
{
if (x&==pre) break;
if (x&) cnt++;
pre=x&;
x>>=;
}
if (x== && cnt<=K)
state[++m]=i,num[m]=cnt;
}
} bool check(LL x,LL y)
{
if ((state[x]&state[y])!=) return false;
if (((state[x]<<)&state[y])!=) return false;
if (((state[y]<<)&state[x])!=) return false;
return true;
} int main()
{
scanf("%lld%lld",&N,&K);
Deal();//预处理每一行的可能情况
for (LL i=;i<=m;++i) f[][i][num[i]]=;
for (LL i=;i<=N;++i)
for (LL j=;j<=m;++j)//当前行状态
for (LL k=;k<=m;++k)
for (LL l=num[j];l<=K;++l)
if (check(j,k))
f[i][j][l]+=f[i-][k][l-num[j]];
LL ans=;
for (LL i=;i<=m;++i)
ans+=f[N][i][K];
printf("%lld",ans);
}
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 ...
- 【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
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Descripti ...
- [SCOI2005]互不侵犯(状压DP)
嗝~算是状压DP的经典题了~ #\(\mathcal{\color{red}{Description}}\) 在\(N×N\)的棋盘里面放\(K\)个国王,使他们互不攻击,共有多少种摆放方案.国王能攻 ...
- 【洛谷 P1896】[SCOI2005]互不侵犯(状压dp)
题目链接 题意:在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. 这是道状压\(DP\)好题啊.. ...
随机推荐
- Mac下显示和隐藏隐藏文件的命令
打开终端,输入: 1.defaults write com.apple.finder AppleShowAllFiles -bool true 此命令显示隐藏文件defaults write com. ...
- Spark你需要知道这些
谈到 Spark,我们总是强调它比 Hadoop 更高效.为什么它可以更高效呢?是因为它优先使用内存存储?还是因为它拥有比 MapReduce 更简单高效的计算模型? 与 Hadoop 作业的区别 我 ...
- 宜立方商城中,mvn报错'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.springframework:spring-webmvc:jar报错
'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.springframework:s ...
- git相关的简单命令
初次使用建议看这个ppt:http://www.bootcss.com/p/git-guide/ 从现有仓库克隆 这需要用到 git clone 命令.如果你熟悉其他的 VCS 比如 Subve ...
- JAVA成员方法的调用分析
如下面例子: public class A { int x=10; public int getx() {return x;} } public class B extends A { int x=2 ...
- python 查询数据库返回的数据类型
self.conn=MySQLdb.connect(host='localhost',port=3306, user='keystone', passwd='OptValley@4312', db=s ...
- [h5+api]移动app开发用到的微信好友,朋友圈,qq好友,新浪微博分享合集
适用H5+环境,能够使用plus方法的移动app中 /** * Created by HBuilder. * User: tyx * Date: 2018-11-21 * Time: 17:28:51 ...
- windows 查看端口
windowsnetstat命令查看进程:netstat -ano查看占用端口进程:netstat -ano|findstr “端口号”,例子netstat -ano|findstr “8080”.t ...
- css 画图形大全
Square #square { width: 100px; height: 100px; background: red; } Rectangle #rectangle { width: 2 ...
- IntelliJ IDEA 2016 完美破解+汉化补丁
百度网盘下载地址:链接:https://pan.baidu.com/s/1cB6XY-N02eRKgGs1kzvqKQ 密码:6pse IntelliJ IDEA 2016是一款JAVA语言发开发工具 ...