您的位置:首页 > 其它

hdoj5093Battle ships【二分图最大匹配】

2016-01-08 13:56 316 查看


Battle ships

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1055    Accepted Submission(s): 373


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

2014上海全国邀请赛——题目重现(感谢上海大学提供题目)

 
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=55;
int n,m;
int numy[maxn];
bool visy[maxn];
bool vis[maxn*maxn];
int p[maxn*maxn];
char map[maxn][maxn];
int G[maxn*maxn][maxn];
bool find(int k){
for(int i=0;G[k][i]!=-1;++i){
if(vis[G[k][i]])continue;
vis[G[k][i]]=true;
if(p[G[k][i]]==-1||find(p[G[k][i]])){
p[G[k][i]]=k;
return true;
}
}
return false;
}
int main()
{
int t,i,j,k;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(i=0;i<n;++i){
scanf("%s",map[i]);
}
for(i=0;i<m;++i){
numy[i]=i;
}
memset(G,-1,sizeof(G));
memset(p,-1,sizeof(p));
memset(visy,false,sizeof(visy));
int numx=0,num=0,cnt=0;
for(i=0;i<n;++i){
for(j=0;j<m;++j){
if(map[i][j]=='*'){
visy[j]=true;
G[numx][cnt++]=numy[j];
}
else if(map[i][j]=='#'){
if(cnt!=0){
cnt=0;
numx++;
}
if(visy[j]){
numy[j]=m+num;num++;
visy[j]=false;
}
}
}
if(cnt){
cnt=0;numx++;
}
}
int ans=0;
for(i=0;i<numx;++i){
memset(vis,false,sizeof(vis));
if(find(i))ans++;
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdoj5093Battle ships