当前位置:网站首页>378. 骑士放置
378. 骑士放置
2022-06-24 19:42:00 【追寻远方的人】
给定一个 N×M 的棋盘,有一些格子禁止放棋子。
问棋盘上最多能放多少个不能互相攻击的骑士(国际象棋的“骑士”,类似于中国象棋的“马”,按照“日”字攻击,但没有中国象棋“别马腿”的规则)。
输入格式
第一行包含三个整数 N,M,T,其中 T 表示禁止放置的格子的数量。
接下来 T 行每行包含两个整数 x 和 y,表示位于第 x 行第 y 列的格子禁止放置,行列数从 1 开始。
输出格式
输出一个整数表示结果。
数据范围
1≤N,M≤100
输入样例:
2 3 0
输出样例:
4
代码:
/* 最大独立集 选出最多的点 使得选出的点之间没有边 在二分图中 总共n个点 求最大独立集n-m(越大越好)则去掉的点数m越小越好 <=> 去掉最少的点(m个),将所有边都破坏掉 <=> 找最小点覆盖所有m条边 <=> 找最大匹配m */
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
typedef pair<int, int> PII;
int n, m, k;
PII match[N][N];
bool g[N][N], st[N][N];
int dx[8] = {
-2, -1, 1, 2, 2, 1, -1, -2};
int dy[8] = {
1, 2, 2, 1, -1, -2, -2, -1};
bool find(int x, int y)
{
for (int i = 0; i < 8; i++)
{
int a = x + dx[i], b = y + dy[i];
if (a < 1 || a > n || b < 1 || b > m)
continue;
if (g[a][b] || st[a][b])
continue;
st[a][b] = 1;
PII t = match[a][b];
if (t.first == 0 || find(t.first, t.second))
{
match[a][b] = {
x, y};
return true;
}
}
return false;
}
int main()
{
cin >> n >> m >> k;
for (int i = 0; i < k; i++)
{
int x, y;
cin >> x >> y;
g[x][y] = true;
}
int res = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if ((i + j) % 2 || g[i][j])
continue;
memset(st, 0, sizeof st);
if (find(i, j))
res++;
}
}
cout << n * m - k - res << endl;
return 0;
}
边栏推荐
- Theoretical analysis of countermeasure training: adaptive step size fast countermeasure training
- Dig deep into MySQL - resolve the non clustered index of MyISAM storage engine
- Dynamic menu, auto align
- EPICS记录参考2--EPICS过程数据库概念
- 【js】-【数组、栈、队列、链表基础】-笔记
- Docker installation redis- simple without pit
- Building Survey [2]
- sql -CONVERT函数
- Laravel pagoda security configuration
- Tech talk activity review kubernetes skills of cloud native Devops
猜你喜欢

Tech talk activity review kubernetes skills of cloud native Devops

并发之共享模型管程
![[Wuhan University] information sharing of the first and second postgraduate entrance examinations](/img/ec/884e656a921e20a5679a2960c9ac4d.jpg)
[Wuhan University] information sharing of the first and second postgraduate entrance examinations

Laravel pagoda security configuration

laravel 宝塔安全配置

Online group chat and dating platform test point

canvas 实现图片新增水印

Attention, postgraduate candidates! They are the easiest scams to get caught during the preparation period?!

Concurrent shared model management
![[text data mining] Chinese named entity recognition: HMM model +bilstm_ CRF model (pytoch) [research and experimental analysis]](/img/8d/7e4bec3d8abaa647fca7462f127c40.png)
[text data mining] Chinese named entity recognition: HMM model +bilstm_ CRF model (pytoch) [research and experimental analysis]
随机推荐
Epics record reference 4 -- fields for all input records and fields for all output records
Laravel message queue
剑指 Offer 13. 机器人的运动范围
laravel 定时任务
Tech Talk 活动回顾|云原生 DevOps 的 Kubernetes 技巧
常用正则表达式
[untitled]
花房集团二次IPO:成于花椒,困于花椒
Selection (027) - what is the output of the following code?
对抗训练理论分析:自适应步长快速对抗训练
Research and investment strategy report on China's bridge anticorrosive coating industry (2022 Edition)
Second IPO of Huafang group: grown up in Zanthoxylum bungeanum, trapped in Zanthoxylum bungeanum
Pousser l'information au format markdown vers le robot nail
laravel model 注意事项
Financial management [5]
EPICS记录参考2--EPICS过程数据库概念
文件包含漏洞问题
go Cobra命令行工具入门
【nvm】
Research and investment strategy report on China's building steel structure anticorrosive coating industry (2022 Edition)