Robert is a famous engineer. One day he was given a task by his boss. The background of the task was the following:
Given a map consisting of square blocks. There were three kinds of blocks: Wall, Grass, and Empty. His boss wanted to place as many robots as possible in the map. Each robot held a laser weapon which could shoot to four directions (north, east, south, west) simultaneously. A robot had to stay at the block where it was initially placed all the time and to keep firing all the time. The laser beams certainly could pass the grid of Grass, but could not pass the grid of Wall. A robot could only be placed in an Empty block. Surely the boss would not want to see one robot hurting another. In other words, two robots must not be placed in one line (horizontally or vertically) unless there is a Wall between them.
Now that you are such a smart programmer and one of Robert's best friends, He is asking you to help him solving this problem. That is, given the description of a map, compute the maximum number of robots that can be placed in the map.
Input
The first line contains an integer T (<= 11) which is the number of test cases.
For each test case, the first line contains two integers m and n (1<= m, n <=50) which are the row and column sizes of the map. Then m lines follow, each contains n characters of '#', '*', or 'o' which represent Wall, Grass, and Empty, respectively.
Output
For each test case, first output the case number in one line, in the format: "Case :id" where id is the test case number, counting from 1. In the second line just output the maximum number of robots that can be placed in that map.
Sample Input
2
4 4
o***
*###
oo#o
***o
4 4
#ooo
o#oo
oo#o
***#
Sample Output
Case :1
3
Case :2
5
但是如果之间有#墙壁的话 就可以
问最多放多少个机器人
问题可以转化为 在二部图中找没有公共顶点的最大边集
主要是建边 以行和列来构造二部图
根据有木有墙壁将行和列分块 如果有空地 则将这块空地所属的行和列所属的块数进行建边
所以需要记录行和列分别有多少块 且这个点所属于那一块
下面是两个写匈牙利算法的代码 都可以
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#define eps 1e-8
#define op operator
#define MOD 10009
#define MAXN 100100
#define INF 0x7fffffff
#define MEM(a,x) memset(a,x,sizeof a)
#define ll __int64
using namespace std;
int n,m;
char mp[60][60];
int vis[3600];
int ans;
int nx,ny;
int x[60][60],y[60][60];
int map[51*51][51*51];
int lx[3600],ly[3600];
bool dfs(int u)
{
for(int i=1;i<=ny;i++)
{
if(map[u][i]&&!vis[i])
{
vis[i]=1;
if(!ly[i]||dfs(ly[i]))
{
ly[i]=u;
lx[u]=i;
return 1;
}
}
}
return 0;
}
void hungary()
{
MEM(lx,0); MEM(ly,0);
for(int i=1;i<=nx;i++)
{
if(!lx[i])
{
MEM(vis,0);
if(dfs(i))
ans++;
}
}
}
int main()
{
//freopen("ceshi.txt","r",stdin);
int tc;
scanf("%d",&tc);
int cs=1;
while(tc--)
{
scanf("%d%d",&n,&m);
// getchar();
for(int i=0;i<n;i++)
scanf("%s",mp[i]);
MEM(x,0); MEM(y,0);
int num=0;
for(int i=0;i<n;i++)
{
int flag=0;
for(int j=0;j<m;j++)
{
if(mp[i][j]=='o')
{
if(!flag) num++;
// cout<<"i:"<<i<<" j:"<<j<<endl;
// cout<<"num "<<num<<endl;
x[i][j]=num; flag=1;
}
else if(mp[i][j]=='#')
flag=0;
}
}
nx=num;
num=0;
for(int i=0;i<m;i++)
{
int flag=0;
for(int j=0;j<n;j++)
{
if(mp[j][i]=='o')
{
if(!flag) num++;
y[j][i]=num; flag=1;
}
else if(mp[j][i]=='#')
flag=0;
}
}
ny=num;
MEM(map,0);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(x[i][j])
{
// cout<<"xxx "<<x[i][j]<<" yyy "<<y[i][j]<<endl;
map[x[i][j]][y[i][j]]=1;
}
}
}
ans=0;
hungary();
printf("Case :%d\n",cs++);
printf("%d\n",ans);
}
return 0;
}
//#include <cstdio>
//#include <iostream>
//#include <cstring>
//#include <cmath>
//#include <algorithm>
//#include <string.h>
//#include <string>
//
//#define eps 1e-8
//#define op operator
//#define MOD 10009
//#define MAXN 100100
//#define INF 0x7fffffff
//#define MEM(a,x) memset(a,x,sizeof a)
//#define ll __int64
//
//using namespace std;
//
//int n,m;
//char mp[60][60];
//int vis[3600];
//int ans;
//int nx,ny;
//int x[60][60],y[60][60];
//int map[51*51][51*51];
//int link[3600];
//
//bool dfs(int u)
//{
// for(int i=1;i<=ny;i++)
// {
// if(map[u][i]&&!vis[i])
// {
// vis[i]=1;
// if(link[i]==-1||dfs(link[i]))
// {
// link[i]=u;
// return 1;
// }
// }
// }
// return 0;
//}
//
//void hungary()
//{
// MEM(link,-1);
// for(int i=1;i<=nx;i++)
// {
// MEM(vis,0);
// if(dfs(i)) ans++;
// }
//}
//
//
//int main()
//{
freopen("ceshi.txt","r",stdin);
// int tc;
// scanf("%d",&tc);
// int cs=1;
// while(tc--)
// {
// scanf("%d%d",&n,&m);
getchar();
// for(int i=0;i<n;i++)
// scanf("%s",mp[i]);
// MEM(x,0); MEM(y,0);
// int num=0;
// for(int i=0;i<n;i++)
// {
// int flag=0;
// for(int j=0;j<m;j++)
// {
// if(mp[i][j]=='o')
// {
// if(!flag) num++;
cout<<"i:"<<i<<" j:"<<j<<endl;
cout<<"num "<<num<<endl;
// x[i][j]=num; flag=1;
// }
// else if(mp[i][j]=='#')
// flag=0;
// }
// }
// nx=num;
// num=0;
// for(int i=0;i<m;i++)
// {
// int flag=0;
// for(int j=0;j<n;j++)
// {
// if(mp[j][i]=='o')
// {
// if(!flag) num++;
// y[j][i]=num; flag=1;
// }
// else if(mp[j][i]=='#')
// flag=0;
// }
// }
// ny=num;
// MEM(map,0);
// for(int i=0;i<n;i++)
// {
// for(int j=0;j<m;j++)
// {
// if(x[i][j])
// {
cout<<"xxx "<<x[i][j]<<" yyy "<<y[i][j]<<endl;
// map[x[i][j]][y[i][j]]=1;
// }
//
// }
// }
// ans=0;
// hungary();
// printf("Case :%d\n",cs++);
// printf("%d\n",ans);
// }
// return 0;
//}