Battle ships
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 521 Accepted Submission(s): 213
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.
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.
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
就是安排最多的船 每行每列只能有一个 但是如果有冰山的话 在冰山两边可以各放一个
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
char ch[100][100];
vector<int> mp[3000];
int link[3000];
int vis[3000];
int x[100][100],y[100][100];
int nx,ny;
int ans;
bool dfs(int u)
{
for(int i=0;i<mp[u].size();i++)
{
int v=mp[u][i];
if(!vis[v])
{
vis[v]=1;
if(link[v]==-1||dfs(link[v]))
{
link[v]=u;
return 1;
}
}
}
return 0;
}
void hungary()
{
MEM(link,-1);
for(int i=0;i<=nx;i++)
{
MEM(vis,0);
if(dfs(i))
ans++;
}
}
int main()
{
// fread;
int tc;
scanf("%d",&tc);
while(tc--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%s",ch[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(ch[i][j]=='*')
{
if(flag==0) num++;
x[i][j]=num; flag=1;
}
if(ch[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(ch[j][i]=='*')
{
if(flag==0) num++;
y[j][i]=num; flag=1;
}
if(ch[j][i]=='#')
flag=0;
}
}
ny=num;
MEM(mp,0);
// for(int i=0;i<=nx;i++)
// mp[i].clear();
// for(int i=0;i<n;i++)
// {
// for(int j=0;j<m;j++)
// cout<<x[i][j];
// cout<<endl;
// }
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
if(x[i][j])
mp[x[i][j]].push_back(y[i][j]);
}
ans=0;
hungary();
printf("%d\n",ans);
}
return 0;
}