Battle ships
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代表冰水,#代表冰山,要想在海域中放置船,保证船与船之间不能相互看到,之间只要有山就不能看到,问最多能放多少船。
解题思路:
比赛的时候没有想出来,看了别人的博客也是花了很长时间才想明白为什么是二分匹配(在这里谢谢这些大牛,感谢你们的blog,要不我现在还不会呢。。。)。
姑且将一片最多只能放一个船的连续网格叫做‘块’。
以样例一为例
首先只考虑行,将每个块标号:将答案存入num1
1000
0000
2203
0004
再此只考虑列,将每个块标号:将答案存入num2
1000
0000
1203
0003
那么对于任意一个可以放船的二维坐标A(i,j),假设其放船,那么num1与num2中与A(i,j)对应num相同的坐标都不能再放船。相当于A所在的行块和列块实现了一一映射关系。
所以将行块看做一个集合,列块看做一个集合。
所求的最大放船数就是两个集合之间的最大匹配数。
匈牙利模版解决问题。
Code:
/*************************************************************************
> File Name: shanghai_1004.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年11月04日 星期二 01时28分18秒
************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<set>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<climits>
#define MAXN 100000
using namespace std;
bool edge[][];
char map[][];
int num1[][],num2[][],link[];
bool vis[];
int k1,k2,cnt;
bool dfs(int x)
{
for (int y=; y<k2; y++)
if (edge[x][y]&&!vis[y])
{
vis[y]=;
if (link[y]==||dfs(link[y]))
{
link[y]=x;
return true;
}
}
return false;
}
void search()
{
//cout<<"1"<<endl;
memset(link,,sizeof(link));
for (int x=; x<k1; x++)
{
memset(vis,,sizeof(vis));
if (dfs(x))
cnt++;
}
}
int main()
{
int T;
cin>>T;
while (T--)
{
int N,M;
cin>>M>>N;
//cout<<M<<N;
memset(edge,,sizeof(edge));
memset(num1,,sizeof(num1));
memset(num2,,sizeof(num2));
for (int i=; i<=M; i++)
for (int j=; j<=N; j++)
cin>>map[i][j];
k1=k2=;
for (int i=; i<=M; i++)
{
for (int j=; j<=N; j++)
{
if (map[i][j]=='#') k1++;
if (map[i][j]=='*') num1[i][j]=k1;
}
k1++; //注意k累加的位置,放在for循环之后保证最后的块
} //的编号为k-1
for (int i=; i<=N; i++)
{
for (int j=; j<=M; j++)
{
if (map[j][i]=='#') k2++;
if (map[j][i]=='*') num2[j][i]=k2;
}
k2++;
}
for (int i=; i<=M; i++)
for (int j=; j<=N; j++)
edge[num1[i][j]][num2[i][j]]=;
cnt=;
search();
cout<<cnt<<endl;
}
return ;
}