Problem Description
Dear contestant, now you are an excellent navy commander, who is responsible of a tough mission currently.
Your fleet unfortunately encountered an enemy fleet near the South Pole where the geographical conditions are negative for both sides. The floating ice and iceberg blocks battleships move which leads to this unexpected engagement highly dangerous, unpredictable and incontrollable.
But, fortunately, as an experienced navy commander, you are able to take opportunity to embattle the ships to maximize the utility of cannons on the battleships before the engagement.
The target is, arrange as many battleships as you can in the map. However, there are three rules so that you cannot do that arbitrary:
A battleship cannot lay on floating ice
A battleship cannot be placed on an iceberg
Two battleships cannot be arranged in the same row or column, unless one or more icebergs are in the middle of them.
Input
There is only one integer T (0<T<12) at the beginning line, which means following T test cases.
For each test case, two integers m and n (1 <= m, n <= 50) are at the first line, represents the number of rows and columns of the battlefield map respectively. Following m lines contains n characters iteratively, each character belongs to one of ‘#’, ‘*’, ‘o’, that symbolize iceberg, ordinary sea and floating ice.
Output
For each case, output just one line, contains a single integer which represents the maximal possible number of battleships can be arranged.
Sample Input
2 4 4 *ooo o### **#* ooo* 4 4 #*** *#** **#* ooo#
Sample Output
3 5
Source
Recommend
hujie
思路:二分匹配求最小点覆盖,每行没被#段断开的连续段拆为多行,列也如此,然后求最大匹配
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<queue>
using namespace std;
#define maxn 105
int n,m,line[maxn*maxn],han[maxn][maxn],lie[maxn][maxn],id,ie,cnt;char mp[maxn][maxn];
struct Edge
{
int to,next;
}edge[maxn*maxn];
int head[maxn*maxn],vis[maxn*25];
void addedge(int u,int v)
{
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int dfs(int u)
{
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(vis[v]==0)
{
vis[v]=1;
if(line[v]==-1||dfs(line[v])==1)
{
line[v]=u;
return 1;
}
}
}
return 0;
}
int slove()
{
int ans=0;
for(int i=1;i<=id;i++)
{
memset(vis,0,sizeof(vis));
ans+=dfs(i);
}
return ans;
}
void init()
{
memset(head,-1,sizeof(head));
memset(line,-1,sizeof(line));
cnt=0;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;id=1;
init();
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf(" %s",mp[i]+1);
for(int j=1;j<=m;j++)
{
if(mp[i][j]!='#') han[i][j]=id;
else id++;
//printf("han[%d][%d]=%d ",i,j,han[i][j]);
}
id++;
//printf("\n");
}
ie=1;
for(int j=1;j<=m;j++)
{
for(int i=1;i<=n;i++)
{
if(mp[i][j]!='#')
{
lie[i][j]=ie;
//addedge(han[i][j],ie);
}
else ie++;//printf("lie[%d][%d]=%d ",i,j,lie[i][j]);
}
ie++;
//printf("\n");
}
for(int j=1;j<=m;j++)
{
for(int i=1;i<=n;i++)
{
if(mp[i][j]=='*')
{
addedge(han[i][j],lie[i][j]);
}
}
}
int ans=slove();
printf("%d\n",ans);
}
}