http://acm.hdu.edu.cn/showproblem.php?pid=5093
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
思路:
给定一个棋盘*可以放棋子,#和O不能放棋子,每一行每一列只能有一个棋子,
若同一行或同一列中的两个棋子之间有#则可以同时出现,问最多可以放多少棋子
思路:
匹配的原则是行和列匹配,若同一行或同一列有两个或两个以上的棋子可以放,
那么将行或列进行拆分编号,最后用匈牙利算法跑最大匹配。
代码:
#include<bits/stdc++.h>
using namespace std;
char a[105][105];
int hang[150][150],lie[150][150];
const int maxn=2620;
int n,m,p,q;
int g[maxn][maxn];
int linker[maxn];
int used[maxn];
bool dfs(int u)
{
for(int v=1;v<=q;v++)
{
if(g[u][v]&&!used[v])
{
used[v]=true;
if(linker[v]==-1||dfs(linker[v]))
{
linker[v]=u;
return true;
}
}
}
return false;
}
int hungary()
{
int res=0;
int u;
memset(linker,-1,sizeof(linker));
for(int u=1;u<=p;u++)
{
memset(used,0,sizeof(used));
if(dfs(u))
res++;
}
return res;
}
void creat_row()
{
memset(hang,0,sizeof(hang));
p=1;
bool flag;
for(int i=1; i<=n; i++)
{
flag=0;
for(int j=1; j<=m; j++)
{
if(a[i][j]=='*')
{
hang[i][j]=p;
flag=1;
}
if(a[i][j]=='#')
{
p++;
flag=0;
}
}
if(flag) p++;
}
}
void creat_column()
{
memset(lie,0,sizeof(lie));
q=1;
bool flag;
for(int j=1; j<=m; j++)
{
flag=0;
for(int i=1; i<=n; i++)
{
if(a[i][j]=='*')
{
lie[i][j]=q;
flag=1;
}
if(a[i][j]=='#')
{
q++;
flag=0;
}
}
if(flag) q++;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
{
scanf("%s",a[i]+1);
}
creat_column();
creat_row();
memset(g,0,sizeof(g));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(a[i][j]=='*')
{
g[hang[i][j]][lie[i][j]]=1;
}
}
}
printf("%d\n",hungary());
}
return 0;
}