AcWing 372. 棋盘覆盖
原题链接
中等
作者:
acwing_kai
,
2020-09-12 16:50:43
,
所有人可见
,
阅读 469
#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
int n, t;
int dx[4] = {0,0,1,-1}, dy[4] = {1,-1,0,0};
bool valid[maxn][maxn], vis[maxn][maxn];
int match[maxn][maxn]; //match = xxxxxx = x * 1000 + y
bool ok(int x, int y){
if(x >= 1 && x <= n && y >= 1 && y <= n && valid[x][y] == 0) return true;
return false;
}
bool dfs(int x, int y){
for(int i = 0; i < 4; ++ i){
int xx = x + dx[i], yy = y + dy[i];
if(ok(xx,yy) && vis[xx][yy] == 0){
vis[xx][yy] = 1;
if(match[xx][yy] == 0 || dfs(match[xx][yy] / 1000, match[xx][yy] % 1000)){
match[xx][yy] = x * 1000 + y;
return true;
}
}
}
return false;
}
int main(){
cin >> n >> t;
for(int i = 1, x, y; i <= t; ++ i){
cin >> x >> y;
valid[x][y] = 1;
}
int ans = 0;
for(int i = 1; i <= n; ++ i)
for(int j = 1; j <= n; ++ j)
if((i + j) % 2 == 0 && valid[i][j] == 0){
memset(vis, 0, sizeof(vis));
if (dfs(i, j)) ans++;
}
cout << ans << endl;
}