Battle ships
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 172 Accepted Submission(s): 84
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
题意 : 给你一张 n*m 的矩阵图形 ,‘ * ’ 表示 空地 , ‘ o ’ 表示浮冰 , ‘ # ’ 表示 冰山 , 你
可以在空地上方战舰 , 但是同一行或同一列只能放一个战舰。若两个战舰想放在同一行或同一
列上,那么它们之间必须要有冰山相隔。然后问你在这张图上最多可以放多少个战舰 ?
思路 : 二部图的最大匹配问题 ,将行和列方别当成一个集合 ,每一行和每一列方别当成对应
集合的点。若行或列中有 ’ # ‘ ,就把行或列拆点。 就以样例 一为例:
*ooo 行集合(将 ’ * ‘标号) 1ooo 列集合 1ooo
o### o### o###
**#* 22#3 23#4
ooo* ooo4 ooo4
然后看原图 ’ * ‘ 的位置,就可以建图了 。
然后求出图的最大匹配就是答案了。
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int N = 55*30; const int maxn = 55; int a[maxn][maxn],b[maxn][maxn],match[N],n,m,p,q; bool con[N][N],vis[N]; char ch[maxn][maxn]; void initial() { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(con,0,sizeof(con)); memset(match,-1,sizeof(match)); } void input() { scanf("%d %d",&n,&m); for(int i=0; i<n; i++) scanf("%s",ch[i]); } void creat_row() { p=1; bool flag; for(int i=0; i<n; i++) { flag=0; for(int j=0; j<m; j++) { if(ch[i][j]=='*') { a[i][j]=p; flag=1; } if(ch[i][j]=='#') { p++; flag=0; } } if(flag) p++; } } void creat_column() { q=1; bool flag; for(int j=0; j<m; j++) { flag=0; for(int i=0; i<n; i++) { if(ch[i][j]=='*') { b[i][j]=q; flag=1; } if(ch[i][j]=='#') { q++; flag=0; } } if(flag) q++; } } bool dfs(int x) { for(int i=1;i<q;i++) { if(!vis[i] && con[x][i]) { vis[i]=1; if(match[i]==-1 || dfs(match[i])) { match[i]=x; return true; } } } return false; } void solve() { creat_row(); creat_column(); for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(ch[i][j]=='*') con[a[i][j]][b[i][j]]=1; int cnt=0; for(int i=1;i<p;i++) { memset(vis,0,sizeof(vis)); if(dfs(i)) cnt++; } printf("%d\n",cnt); } int main() { int T; scanf("%d",&T); while(T--) { initial(); input(); solve(); } return 0; }