当前位置:网站首页>洛谷P1896 互不侵犯
洛谷P1896 互不侵犯
2022-07-27 10:33:00 【ThXe】
题目描述
题目链接
在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案。国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子。
输入格式
只有一行,包含两个数N,K ( 1 <=N <=9, 0 <= K <= N * N)
输出格式
所得的方案数
#include<bits/stdc++.h>
using namespace std;
int sat[2000], siz[2000];//sat 一行的合法状态 siz当前状态有多少个国王
int cnt = 0;
int n, m;
long long f[10][2000][100] = {
0 };//f[i][j][k]第i行 状态为j 放k个国王的方案数
void dfs(int Sta, int now_size, int i_th)//预处理出每一个状态 Sta 代表状态 00000 now_size代表当前已经有多少个国王 i_th代表当前是在用第几列的格子
{
if (i_th >= n)//如果已经处理完毕(注意是大于等于)
{
sat[++cnt] = Sta;//处理第cnt个状态
siz[cnt] = now_size;//第cnt个状态可以放国王的数量为 now_size个
return;//新建一个状态
}
dfs(Sta, now_size, i_th + 1);//不用第i_th个格子
dfs(Sta + (1 << i_th), now_size + 1, i_th + 2);//用第i_th个格子,此时i_th要加2,即为跳过下一个格子 因为左右不能相邻
}
int main()
{
scanf("%d%d", &n, &m);
dfs(0, 0, 0);
for (int i = 1; i <= cnt; i++)f[1][i][siz[i]] = 1;//第一层的所有状态均是有1种情况的
for (int i = 2; i <= n; i++)
for (int j = 1; j <= cnt; j++)//上一层的状态为j
for (int k = 1; k <= cnt; k++)//当前层的状态为k
{
if (sat[j] & sat[k])continue;//上下相临
if ((sat[j] << 1) & sat[k])continue;//右上相邻
if (sat[j] & (sat[k] << 1))continue;//左上相邻
for (int s = m; s >= siz[j]; s--)f[i][j][s] += f[i - 1][k][s - siz[j]];//枚举s,计算f[i][j][s]
}
long long ans = 0;
for (int i = 1; i <= cnt; i++)ans += f[n][i][m];//统计最终答案,记得用long long
printf("%lld", ans);
return 0;
}
边栏推荐
- IO流_数据输入输出流的概述和讲解
- 8 find subsequences with a maximum length of K
- The permission problem of Oracle operating openldap
- Object array de duplication
- Application scenarios, key technologies and network architecture of communication perception integration
- [FPGA tutorial case 40] communication case 10 -- Verilog implementation of a simple OFDM system based on FPGA
- Real time development platform construction practice, in-depth release of real-time data value - 04 live broadcast review
- ASP. Net core dependency injection journey: 1. Theoretical concepts
- Recruit top talents! The "megeagle creator program" of Kuangshi technology was officially launched
- 计算重叠积分的第二种方法
猜你喜欢

The difference between scalar, vector, matrix and tensor in deep learning

tensorflow运行报错解决方法

Play with the cluster configuration center and learn about the Taier console

Webrtc realizes simple audio and video call function

Li Hongyi_ Machine learning_ Assignment 4 (detailed explanation)_ HW4 Classify the speakers

发动机悬置系统冲击仿真-瞬时模态动态分析与响应谱分析

DNS principle and resolution process

Iptables prevent nmap scanning and binlog explanation

荒野觅踪---寻找迭代次数

Openatom openharmony sub forum, see you today at 14:00! Wonderful release of memorabilia attached
随机推荐
Yum source installation
Shortest moving distance and entropy of morphological complex
Symmetric encryption and asymmetric encryption
MIMO array 3D imaging technology based on mobile terminal
antd table+checkbox 默认值显示
10 complete half of the questions
9 UAV array
tensorflow运行报错解决方法
IO stream_ Character stream, IO stream summary, IO stream case summary
What changes will metauniverse bring to the music industry in the trillion market?
如何组装一个注册中心
No identifier specified for entity solution
Study notes Minio
SQL Server2000 database error
Integrated design of communication perception based on CSI: problems, challenges and Prospects
学习笔记-minio
ECCV 2022 | complete four tracking tasks at the same time! Unicorn: towards the unification of target tracking
NFT leaderboard -nft real offer latest address: NFT leaderboard.com
Background color style modification on table hover in antd
Using skills of word