大意:给定一个MxN大小的方格,地图有3中方格,墙、草地、空地。他的老板希望Robert能在地图中放置尽可能多的机器人。每个机器人可以向四个方向开火,激光可以穿透草地,但不能穿透墙壁。
思路:将一行被墙隔开且包含空地的连续区域叫做“块”。
把每个横向“块”看做二部图中的X中的顶点,竖向“块”看做集合中Y的顶点,若两个“块”有公共的顶点空地,于是就连一条边。这样就转换成了没有公共顶点的最大边集,即最大匹配。
我们怎么去求“块”呢?用一个2个二维数组xs,ys来对水平方向和垂直方向上的“块”进行编号,编号之后如果两个块有公共的空地的话,那么就在“块”与“块”之间连边。
即read_graph(xs[i][j], ys[i][j]);
CODE:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 60;
const int MAXM = 3600;
struct Edge
{
int v, next;
}edge[MAXM];
char map[MAXN][MAXN];
int first[MAXM], link2[MAXM];
bool vis[MAXM];
int n, m;
int cnt;
int xn;
int xs[MAXN][MAXN], ys[MAXN][MAXN];
inline void init()
{
cnt = 0;
memset(first, -1, sizeof(first));
memset(link2, -1, sizeof(link2));
memset(xs, 0, sizeof(xs));
memset(ys, 0, sizeof(ys));
}
inline void read_graph(int u, int v)
{
edge[cnt].v = v;
edge[cnt].next = first[u], first[u] = cnt++;
}
bool ED(int u)
{
for(int e = first[u]; e != -1; e = edge[e].next)
{
int v = edge[e].v;
if(!vis[v])
{
vis[v] = 1;
if(link2[v] == -1 || ED(link2[v]))
{
link2[v] = u;
return true;
}
}
}
return false;
}
inline void read_graph2()
{
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++) scanf("%s", map[i]);
int tot = 0;
for(int i = 0; i < n; i++)
{
int flag = 0;
for(int j = 0; j < m; j++)
{
if(map[i][j] == 'o')
{
if(flag == 0) tot++;
xs[i][j] = tot;
flag = 1;
}
else if(map[i][j] == '#') flag = 0;
}
}
xn = tot; tot = 0;
for(int j = 0; j < m; j++)
{
int flag = 0;
for(int i = 0; i < n; i++)
{
if(map[i][j] == 'o')
{
if(flag == 0) tot++;
ys[i][j] = tot;
flag = 1;
}
else if(map[i][j] == '#') flag = 0;
}
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
int u = xs[i][j], v = ys[i][j];
if(u && v)
{
read_graph(u, v);
}
}
}
}
void solve()
{
int ans = 0;
for(int i = 1; i <= xn; i++)
{
memset(vis, 0, sizeof(vis));
if(ED(i)) ans++;
}
printf("%d\n", ans);
}
int main()
{
int T, times = 0;
scanf("%d", &T);
while(T--)
{
init();
read_graph2();
printf("Case :%d\n", ++times);
solve();
}
return 0;
}
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 60;
const int MAXM = 3600;
struct Edge
{
int v, next;
}edge[MAXM];
char map[MAXN][MAXN];
int first[MAXM], link2[MAXM];
bool vis[MAXM];
int n, m;
int cnt;
int xn;
int xs[MAXN][MAXN], ys[MAXN][MAXN];
inline void init()
{
cnt = 0;
memset(first, -1, sizeof(first));
memset(link2, -1, sizeof(link2));
memset(xs, 0, sizeof(xs));
memset(ys, 0, sizeof(ys));
}
inline void read_graph(int u, int v)
{
edge[cnt].v = v;
edge[cnt].next = first[u], first[u] = cnt++;
}
bool ED(int u)
{
for(int e = first[u]; e != -1; e = edge[e].next)
{
int v = edge[e].v;
if(!vis[v])
{
vis[v] = 1;
if(link2[v] == -1 || ED(link2[v]))
{
link2[v] = u;
return true;
}
}
}
return false;
}
inline void read_graph2()
{
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++) scanf("%s", map[i]);
int tot = 0;
for(int i = 0; i < n; i++)
{
int flag = 0;
for(int j = 0; j < m; j++)
{
if(map[i][j] == 'o')
{
if(flag == 0) tot++;
xs[i][j] = tot;
flag = 1;
}
else if(map[i][j] == '#') flag = 0;
}
}
xn = tot; tot = 0;
for(int j = 0; j < m; j++)
{
int flag = 0;
for(int i = 0; i < n; i++)
{
if(map[i][j] == 'o')
{
if(flag == 0) tot++;
ys[i][j] = tot;
flag = 1;
}
else if(map[i][j] == '#') flag = 0;
}
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
int u = xs[i][j], v = ys[i][j];
if(u && v)
{
read_graph(u, v);
}
}
}
}
void solve()
{
int ans = 0;
for(int i = 1; i <= xn; i++)
{
memset(vis, 0, sizeof(vis));
if(ED(i)) ans++;
}
printf("%d\n", ans);
}
int main()
{
int T, times = 0;
scanf("%d", &T);
while(T--)
{
init();
read_graph2();
printf("Case :%d\n", ++times);
solve();
}
return 0;
}