hdu 4185 二分图匹配

时间:2021-10-04 04:49:10

题意用1*2的木板覆盖矩阵中的‘#’,(木板要覆盖的只能是‘#’),问最多能用几个木板覆盖

将#抽象为二分图的点,一个木板就是一个匹配,注意最后结果要除以2

Sample Input

1
6
......
.##...
.##...
....#.
....##
......
Sample Output
Case 1: 3
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
const int INF=0x3f3f3f3f;
const double eps=1e-;
typedef long long ll;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
const int MAXN=;
int n,m,tt;
char s[MAXN][MAXN];
int uN,vN;//u,v的数目,使用前面必须赋值
int g[MAXN][MAXN];//邻接矩阵
int linker[MAXN];
bool used[MAXN];
int num[MAXN][MAXN];
bool dfs(int u)
{
for(int v=;v<vN;v++)
if(g[u][v]&&!used[v])
{
used[v]=true;
if(linker[v]==-||dfs(linker[v]))
{
linker[v] = u;
return true;
}
}
return false;
}
int hungary()
{
int res=;
memset(linker,-,sizeof(linker));
for(int u=;u<uN;u++)
{
memset(used,false,sizeof(used));
if(dfs(u))res++;
}
return res;
}
int main()
{
int i,j,k;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
int ca=;
scanf("%d",&tt);
while(tt--)
{
scanf("%d",&n);
cl(g);
int tot=;
cl(num);
for(i=;i<n;i++)
{
scanf("%s",&s[i]);
for(j=;j<n;j++)
{
if(s[i][j]=='#') num[i][j]=tot++;
}
} uN=vN=tot;
for(i=;i<n;i++)
{
for(j=;j<n;j++)
{
if(s[i][j]=='#')
{
if(s[i+][j]=='#'&&i+<n) g[num[i][j]][num[i+][j]]=;
if(s[i][j+]=='#'&&j+<n) g[num[i][j]][num[i][j+]]=;
if(s[i-][j]=='#'&&i->=) g[num[i][j]][num[i-][j]]=;
if(s[i][j-]=='#'&&j->=) g[num[i][j]][num[i][j-]]=;
}
}
}
ca++;
printf("Case %d: %d\n",ca,hungary()/);
}
}