#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int MAXN = 10;
int n,nl,nr;
char graph[MAXN][MAXN];
int grow[MAXN][MAXN];
int gcol[MAXN][MAXN];
vector<int> g[MAXN];
int from[MAXN],tot;
int to[MAXN];
bool use[MAXN];
bool match(int x){
for(int i = 0;i < g[x].size();i++)
if(!use[g[x][i]]){
use[g[x][i]] = true;
if(from[g[x][i]] == -1||match(from[g[x][i]])){
from[g[x][i]] = x;
to[x] = g[x][i];
return true;
}
}
return false;
}
int hungary(){
tot = 0;
memset(from,255,sizeof(from));
for(int i = 0;i < n;i++){
memset(use,0,sizeof(use));
if(match(i))
++tot;
}
return tot;
}
void build(){
memset(grow, -1, sizeof(grow));
memset(gcol, -1, sizeof(gcol));
nl = 0;nr = 0;
for(int i = 0;i < n;i++)//ºáÏò±àºÅ
for(int j = 0;j < n;j++){
if(graph[i][j] == '.'&&grow[i][j] == -1){
for(int k = j;graph[i][k] == '.'&&k < n;k++){
grow[i][k] = nl;
}
nl++;
}
}
for(int j = 0;j < n;j++)//×ÝÏò±àºÅ
for(int i = 0;i < n;i++){
if(graph[i][j] == '.'&&gcol[i][j] == -1){
for(int k = i;graph[k][j] == '.'&&k < n;k++){
gcol[k][j] = nr;
}
nr++;
}
}
for(int i = 0;i < n;i++)
for(int j = 0;j < n;j++){
if(graph[i][j] == '.'){
g[grow[i][j]].push_back(gcol[i][j]);
// g[gcol[i][j]].push_back(grow[i][j]);
}
}
n = nl;
/*
for(int i = 0;i < n;i++){
for(int j = 0;j < g[i].size();j++){
printf("%d ",g[i][j]);
}
printf("\n");
}
*/
return;
}
int main(){
while(scanf("%d",&n) != EOF){
if(n == 0)break;
for(int i = 0;i < n;i++){
scanf("%s",graph[i]);
}
for(int i = 0;i < MAXN;i++)
g[i].clear();
build();
printf("%d\n",hungary());
}
return 0;
}
hdu1045 二分图匹配
猜你喜欢
转载自blog.csdn.net/qq_37136305/article/details/83032679
今日推荐
周排行