Battle ships
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1238 Accepted Submission(s): 457
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
Source
题意:给出nxm地图,*代表可以放飞船,o不能放飞船,#代表障碍。同一行和同一列只能允许有一架飞船,但是如果中间有障碍割开即可放多架,给定地图问最多可以放多少飞船。
思路:当时没有思路,后来看了题解发现是二分图匹配的经典题型-行列匹配。即把所有横向相连和纵向相连的可以放飞船并且中间没有障碍的位置合并在一起,给定一个编号。之后对于每个格子,将其横向编号和纵向编号连边,求最大匹配数即是结果。
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
char c[55][55];
int x[55][55];
int y[55][55];
int ma[2550][2550];
int book[2550];
int match[2550];
int cnt,ans;
int dfs(int u){
for(int i=1;i<ans;i++){
if(book[i]==0&&ma[u][i]==1){
book[i]=1;
if(match[i]==0||dfs(match[i])){
match[i]=u;
return 1;
}
}
}
return 0;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m;
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++){
scanf("%s",c[i]);
}
cnt=1;
memset(x,0,sizeof(x));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(c[i][j]=='*'){
while(j<m&&c[i][j]!='#'){
if(c[i][j]=='*'){
x[i][j]=cnt;
}
j++;
}
cnt++;
}
}
cnt++;
}
memset(y,0,sizeof(y));
ans=1;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(c[j][i]=='*'){
while(j<m&&c[j][i]!='#'){
if(c[j][i]=='*'){
y[j][i]=ans;
}
j++;
}
ans++;
}
}
ans++;
}
memset(ma,0,sizeof(ma));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(x[i][j]&&y[i][j]){
ma[x[i][j]][y[i][j]]=1;
}
}
}
memset(match,0,sizeof(match));
int sum=0;
for(int i=1;i<cnt;i++){
for(int j=1;j<ans;j++)
book[j]=0;
if(dfs(i))
sum++;
}
printf("%d\n",sum);
}
return 0;
}