题目链接:Acwing_378骑士放置
知识点:最大独立集=总点数-最大匹配数
AC代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
#define x first
#define y second
const int N = 110;
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]) continue;
if (st[a][b]) continue;
st[a][b] = true;
pii t = match[a][b];
if (t.x == 0 || find(t.x, t.y))
{
match[a][b] = { x,y };
return true;
}
}
return false;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
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;
}