当前位置:网站首页>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;
}
边栏推荐
- Collation of Digital IC design experience (II)
- Simulated 100 questions and online simulated examination of high voltage electrician examination in 2022
- 【js】-【数组、栈、队列、链表基础】-笔记
- laravel 宝塔安全配置
- [laravel series 7.9] test
- Accounting standards for business enterprises application [5]
- Laravel user authorization
- 冒泡排序
- Some updates about a hand slider (6-18, JS reverse)
- vulnhub DC: 2
猜你喜欢

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

Main cause of EMI - mold current
![[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]

How to submit the shopee opening and settlement flow?

File contains vulnerability issues

【js】-【链表-应用】-学习笔记

2022 safety officer-b certificate examination question bank and answers

Detailed explanation of online group chat and dating platform project (servlet implementation)
![[JS] - [linked list - application] - learning notes](/img/e1/76d2a347b05212de349322f43e0b3a.png)
[JS] - [linked list - application] - learning notes

Mousse shares listed on Shenzhen Stock Exchange: becoming popular by mattress and "foreign old man", with a market value of 22.4 billion yuan
随机推荐
Construction equipment [5]
Construction equipment [4]
Selection (027) - what is the output of the following code?
Gocolly manual
Collation of Digital IC design experience (II)
Theoretical analysis of countermeasure training: adaptive step size fast countermeasure training
#22Map介绍与API
监听 Markdown 文件并热更新 Next.js 页面
Websocket learning
Online group chat and dating platform test point
Building Survey [1]
【js】-【树】-学习笔记
How should we measure agile R & D projects?
Docker installation MySQL simple without pit
Use of laravel verifier
How to submit the shopee opening and settlement flow?
07_SpingBoot 实现 RESTful 风格
Financial management [2]
Canvas to add watermark to pictures
Push markdown format information to the nailing robot