当前位置:网站首页>【ACwing 1064 小国王】状压dp
【ACwing 1064 小国王】状压dp
2022-07-28 02:19:00 【宇智波一打七~】
题目链接
题意
你需要在一个n*n的棋盘放k个棋子,使得他们互不攻击,每个国王都会对其相邻的8个位置攻击,现在问你有多少个放置方案
思路
数据量是10,很明显的状压dp题,f[i][j][k]代表的是已经摆好了前i-1行,现在第i行状态为j,一共的棋子数是k的方案数,每一种合法状态我们可以提前预处理出来,还有每种状态能转移的状态也可以预处理出来,然后就是状压dp的部分,有一个地方需要注意,就是状态转移预处理的时候要判断左移和右移的情况,本来我写的只有左移,发现答案比标准答案大,就是预处理的时候能转移的状态增加了,所以又加了一个右移的判断之后就正确了,下面请看代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 12;
vector<long long> hefa;
vector<long long> zhuanyi[1<<N];
long long f[N][1<<N][N*N],cnt[1<<N];
int count(int x){
int ans = 0;
while(x) ans += (x&1),x >>= 1;
return ans;
}
int main(){
int n,k;
cin>>n>>k;
zhuanyi[0].push_back(0);
for(int i=0;i<(1<<n);i++){
if((i&(i>>1)) == 0) hefa.push_back(i);
cnt[i] = count(i);
}
f[0][0][0] = 1;
for(int i=0;i<hefa.size();i++){
for(int j=i+1;j<hefa.size();j++){
long long x = hefa[i],y = hefa[j];
if(((x>>1)&(y)) == 0 && (x&y) == 0 && (x&(y>>1)) == 0) zhuanyi[x].push_back(y),zhuanyi[y].push_back(x);
//就这个地方,要同时判断(x>>1)&(y) == 0和(x&(y>>1)) == 0,不然会出错
}
}
// for(int i=0;i<hefa.size();i++){
// cout<<hefa[i]<<' ';
// for(auto t : zhuanyi[hefa[i]]) cout<<t<<' ';
// cout<<endl;
// }
long long ans = 0;
for(int i=1;i<=n;i++){
for(int s=0;s<=k;s++)
for(auto x : hefa){
for(auto y : zhuanyi[x]){
if(s >= cnt[x])
f[i][x][s] += f[i-1][y][s-cnt[x]];
}
}
}
for(auto t : hefa) ans += f[n][t][k];
cout<<ans<<endl;
return 0;
}
边栏推荐
- QFileDevice、QFile、QSaveFile、QTemporaryFile
- Redis群集
- JVM memory layout detailed, illustrated, well written!
- Record of a cross domain problem
- Interview experience: first tier cities move bricks and face software testing posts. 5000 is enough
- Qt官方示例:Fridge Magnets Example(冰箱贴)
- Opengauss source code, what ide tools are used to manage, edit and debug?
- Data Lake: database data migration tool sqoop
- 42.js -- precompiled
- The applet has obtained the total records and user locations in the database collection. How to use aggregate.geonear to arrange the longitude and latitude from near to far?
猜你喜欢

嵌入式分享合集22

Docker高级篇-Docker容器内Redis集群配置

JS event object 2 e.charcode character code e.keycode key code box moves up, down, left and right

RTSP/Onvif协议EasyNVR视频平台一键升级方案的开发设计逻辑

方案分享 | 高手云集 共同探索重口音AI语音识别

exness:日本物价上涨收入下降,英镑/日元突破 165

Kubernetes-----介绍

Qt官方示例:Fridge Magnets Example(冰箱贴)

tfx airflow 使用体验

Record of a cross domain problem
随机推荐
Design and practice of unified security authentication for microservice architecture
方案分享 | 高手云集 共同探索重口音AI语音识别
Pytest the best testing framework
【stream】并行流与顺序流
[ACNOI2022]总差一步
[stream] parallel stream and sequential stream
Stop paging with offset and limit. The performance is too poor!
Development and design logic of rtsp/onvif protocol easynvr video platform one click upgrade scheme
Vscode debug displays multiple columns of data
基于c8t6芯片开发RC522模块实现呼吸灯
Kubernetes-----介绍
[stream] basic knowledge of stream
clientY vs pageY
P6118 [joi 2019 final] solution to the problem of Zhenzhou City
els 键盘信息
MySQL索引学习
Note that these regions cannot take the NPDP exam in July
每日刷题巩固知识
行业洞察 | 语音识别真的超过人耳朵了吗?
Distributed transaction Senta (I)