题意
题解
每一行、列上被墙分隔的块,至多被一个镭射覆盖;预处理出这样的块,将行块作为左部节点,列块作为右部节点,将空白区域上的点可覆盖的行、列块连边。求解二分图最大匹配即可。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 55, maxv = maxn * maxn, maxe = maxv * maxn;
int T, N, M, fn[maxn][maxn], fm[maxn][maxn], match[maxv];
char mp[maxn][maxn];
int tot, head[maxv], to[maxe], nxt[maxe];
bool vs[maxv];
inline void add(int x, int y) { to[++tot] = y, nxt[tot] = head[x], head[x] = tot; }
bool dfs(int x)
{
for (int i = head[x]; i; i = nxt[i])
{
int y = to[i], w = match[y];
if (vs[y])
continue;
vs[y] = 1;
if (w == -1 || dfs(w))
{
match[y] = x;
return 1;
}
}
return 0;
}
int main()
{
scanf("%d", &T);
for (int c = 1; c <= T; ++c)
{
memset(head, 0, sizeof(head));
memset(match, -1, sizeof(match));
tot = 0;
scanf("%d%d", &N, &M);
for (int i = 0; i < N; ++i)
scanf("%s", mp + i);
int n = 0, m = 0;
for (int i = 0; i < N; ++i)
for (int j = 0; j < M; ++j)
if (mp[i][j] != '#')
{
fn[i][j] = n;
if (j == M - 1 || mp[i][j + 1] == '#')
++n;
}
for (int j = 0; j < M; ++j)
for (int i = 0; i < N; ++i)
if (mp[i][j] != '#')
{
fm[i][j] = m;
if (i == N - 1 || mp[i + 1][j] == '#')
++m;
}
for (int i = 0; i < N; ++i)
for (int j = 0; j < M; ++j)
if (mp[i][j] == 'o')
add(fn[i][j], fm[i][j]);
int res = 0;
for (int i = 0; i < n; ++i)
memset(vs, 0, sizeof(vs)), res += dfs(i);
printf("Case :%d\n%d\n", c, res);
}
return 0;
}