当前位置:网站首页>378. Knight placement
378. Knight placement
2022-06-24 23:21:00 【Searching for people far away】
Given a N×M The chessboard of , There are some squares where chess pieces are forbidden .
Ask the maximum number of knights that can't attack each other on the chessboard ( Chess “ The knight ”, Similar to Chinese chess “ Horse ”, according to “ Japan ” Word attack , But there is no Chinese chess “ Don't be a horse leg ” The rules of ).
Input format
The first line contains three integers N,M,T, among T Indicates the number of cells that cannot be placed .
Next T Each line contains two integers x and y, It means to be in the second place x Xing di y Columns are not allowed to be placed in the grid , The number of rows and columns ranges from 1 Start .
Output format
Output an integer to represent the result .
Data range
1≤N,M≤100
sample input :
2 3 0
sample output :
4
Code :
/* The largest independent set Select the most points So that there is no edge between the selected points In a bipartite graph in total n A little bit Find the maximum independent set n-m( The bigger the better ) Then remove the number of points m The smaller the better. <=> Remove the least points (m individual ), Break all edges <=> Find the minimum point to cover all m side <=> Find the largest match 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;
}
边栏推荐
猜你喜欢
![[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]

斐波那契

并发之共享模型管程

【js】-【栈、队-应用】-学习笔记

Case analysis: using "measurement" to improve enterprise R & D efficiency | ones talk

vulnhub DC: 2

Dig deep into MySQL - resolve the non clustered index of MyISAM storage engine
![[laravel series 7.9] test](/img/49/4b470a8b309bab4a83eed930dcce65.png)
[laravel series 7.9] test

2022 safety officer-b certificate examination question bank and answers
![[JS] - [linked list - application] - learning notes](/img/e1/76d2a347b05212de349322f43e0b3a.png)
[JS] - [linked list - application] - learning notes
随机推荐
对抗训练理论分析:自适应步长快速对抗训练
Tech talk activity review kubernetes skills of cloud native Devops
gocolly-手册
Daily practice (22): maximum sum of continuous subarrays
Mycms we media CMS V3.0, resource push optimization, new free template
RT-thread使用rt-kprintf
JD 618 conference tablet ranking list announced that the new dark horse brand staff will compete for the top three, learning from Huawei, the leader of domestic products
jar中没有主清单属性
Second IPO of Huafang group: grown up in Zanthoxylum bungeanum, trapped in Zanthoxylum bungeanum
laravel学习笔记
golang map clear
OpenSSL SSL_read: Connection was reset, errno 10054
UNION ALL UNION FULL JOIN
宁德时代定增450亿:高瓴认购30亿 曾毓群仍控制23%股权
Learn about redlock
[JS] - [tree] - learning notes
golang map clear
Laravel creates a service layer
01_SpingBoot 框架入门
Some updates about a hand slider (6-18, JS reverse)