【图论】[ZOJ1054]Place the Robots
分析:二分图匹配,和ZOJ1052差不多,但是草地在计算连通块是当做空地,在连边是如果交点是草地则不连边。
代码:
#include<cstdio>
#include<cstring>
#define MAXN 50
int n,a[MAXN+10][MAXN+10],cnt,cnt1,c[MAXN*MAXN*2+10],ans,T,m;
char s[MAXN+10][MAXN+10];
bool vis[MAXN*MAXN*2+10];
struct node{
int v;
node *next;
}edge[MAXN*MAXN*MAXN*MAXN/4+10],*adj[MAXN*MAXN*2+10],*ecnt=edge;
void addedge(int u,int v){
node *p=++ecnt;
p->v=v;
p->next=adj[u];
adj[u]=p;
}
bool dfs(int u){
int v;
for(node *p=adj[u];p;p=p->next){
v=p->v;
if(!vis[v]){
vis[v]=1;
if(!c[v]||dfs(c[v])){
c[u]=v;
c[v]=u;
return 1;
}
}
}
return 0;
}
void maxmatch(){
for(int i=1;i<=cnt1;i++){
memset(vis,0,sizeof vis);
if(!c[i]&&dfs(i))
ans++;
}
}
int main()
{
int i,j,tcnt=0;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
memset(c,0,sizeof c);
memset(adj,0,sizeof adj);
memset(a,0,sizeof a);
ans=cnt=0;
ecnt=edge;
for(i=1;i<=n;i++)
scanf("%s",s[i]+1);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++){
if(s[i][j]=='#')
continue;
if(!s[i][j-1]||s[i][j-1]=='#')
cnt++;
if(s[i][j]=='*')
continue;
a[i][j]=cnt;
}
cnt1=cnt;
for(j=1;j<=m;j++)
for(i=1;i<=n;i++){
if(s[i][j]=='#')
continue;
if(!s[i-1][j]||s[i-1][j]=='#')
cnt++;
if(s[i][j]=='*')
continue;
addedge(a[i][j],cnt);
addedge(cnt,a[i][j]);
}
maxmatch();
printf("Case :%d\n%d\n",++tcnt,ans);
}
}