Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2001 Accepted Submission(s): 726 Problem Description Dear contestant, now you are an excellent navy commander, who is responsible of a tough mission currently. Input There is only one integer T (0<T<12) at the beginning line, which means following T test cases. 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 |
题目大意:你现在是一个船长,很多船的船长,现在你有一份海面上的可以停船的地图,其中有的地方现在是冰川,有的地方是浮冰,有的地方还是普通海面,停船时,每一行每一列只能停一艘船,但是现在给出:若中间由冰川隔开,则可以停靠多艘船,
地图上:# 冰川 ------o 浮冰 ------ *普通海洋
思路:把每一行和每一列的可行位置作为二分图的两个点集,这样二分图的最大匹配数量就是最大停船数量
效果如图:
代码:
#include<cstring>
#include<cstdio>
#include<algorithm>
#define maxn 55
using namespace std;
int map[maxn*55][maxn*55];
char str[maxn][maxn];
int used[maxn*maxn],link[maxn*maxn];
int x[maxn][maxn],y[maxn][maxn];
int x1,y1;
int n,m;
void init()
{
memset(map,0,sizeof(map));
memset(x,0,sizeof(x));
memset(y,0,sizeof(y));
}
void input()
{
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++)
scanf("%s",str[i]);
}
void creatx()
{
x1=1;
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(str[i][j]=='*')
x[i][j]=x1;
if(str[i][j]=='#')
x1++;
}
x1++;
}
return ;
}
void creaty()
{
y1=1;
for(int j=0; j<m; j++)
{
for(int i=0; i<n; i++)
{
if(str[i][j]=='*')
y[i][j]=y1;
if(str[i][j]=='#')
y1++;
}
y1++;
}
return ;
}
void getmap()
{
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
if(str[i][j]=='*')
map[x[i][j]][y[i][j]]=1;
return ;
}
bool dfs(int x)
{
for(int i = 1; i < y1; ++i)
{
if(map[x][i] && !used[i])
{
used[i] = 1;
if(link[i] == -1 || dfs(link[i]))
{
link[i] = x;
return true;
}
}
}
return false;
}
int hungary()
{
int ans = 0;
memset(link, -1, sizeof(link));
for(int j = 1; j < x1; ++j)
{
memset(used, 0, sizeof(used));
if(dfs(j))
ans++;
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
input();
creatx();
/*for(int i = 0; i < n; ++i)
{
for(int j = 0; j < m; ++j)
printf("%d ", x[i][j]);
printf("\n");
}*/
//printf("\n");
creaty();
/*for(int i = 0; i < n; ++i)
{
for(int j = 0; j < m; ++j)
printf("%d ", y[i][j]);
printf("\n");
}*/
getmap();
int ans=hungary();
printf("%d\n",ans);
}
}