当前位置:网站首页>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;
}
边栏推荐
- UNION ALL UNION FULL JOIN
- How to submit the shopee opening and settlement flow?
- 03_SpingBoot 核心配置文件
- Dig deep into MySQL - resolve the non clustered index of MyISAM storage engine
- 376. 机器任务
- Listen to the markdown file and hot update next JS page
- Paddledtx v1.0 has been released, and its security and flexibility have been comprehensively improved!
- #22Map介绍与API
- Collation of Digital IC design experience (II)
- [JS] - [stack, team - application] - learning notes
猜你喜欢
【基础知识】~ 半加器 & 全加器
2022 simulated 100 questions and simulated examination of high-altitude installation, maintenance and demolition
从客户端到服务器
Uncover the secrets of Huawei cloud enterprise redis issue 16: acid'true' transactions beyond open source redis
Record the range of data that MySQL update will lock
Tech talk activity review kubernetes skills of cloud native Devops
Non single file component
【js】-【树】-学习笔记
RT-thread使用rt-kprintf
伪原创智能改写api百度-收录良好
随机推荐
[JS] - [array application] - learning notes
Dig deep into MySQL - resolve the clustered index / secondary index / federated index of InnoDB storage engine
03_ Spingboot core profile
372. 棋盘覆盖
【基础知识】~ 半加器 & 全加器
文件包含漏洞问题
376. 机器任务
golang convert map to json string
golang map clear
2022 simulated 100 questions and simulated examination of high-altitude installation, maintenance and demolition
F29oc analysis
研究生宿舍大盘点!令人羡慕的研究生宿舍来了!
【UVM入门 ===> Episode_8 】~ Sequence 和 Sequencer、Sequence 层次化
02_SpingBoot 入门案例
監聽 Markdown 文件並熱更新 Next.js 頁面
Laravel pagoda security configuration
监听 Markdown 文件并热更新 Next.js 页面
Listen to the markdown file and hot update next JS page
第六章 网络学习相关技巧5(超参数验证)
【js】-【树】-学习笔记