Battle ships HDU - 5093(建图+二分图匹配)

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代表冰水,#代表冰山,要想在海域中放置船

保证船与船之间不能相互看到,问最多能放多少船?

思路:首先想到之前做过的一个题,保证每行和每列不能同时放两首船的题,当时用二分图求解,

现在只不过是增加了限制条件,当中间有#相隔就可以放置多首船,所以这道题用二分图应该可解。

然鹅,太菜~~,不知道如何建图。

将一片最多只能放一个船的连续网格叫做‘块’。
以样例一为例
首先只考虑行,将每个块标号:将答案存入x [ ] [ ]
 1000
 0000
 2203
 0004
再此只考虑列,将每个块标号:将答案存入y [ ][ ]
 1000
 0000
 2304
 0004

二分图的X部,Y部建立完毕,用最大匹配求解

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 55;
char mp[N][N];
vector<int>G[N*55];
int X[N][N],Y[N][N];
int x,y,n,m;
int vis[N*55],match[N*55];
void init(){
	x=y=1;
	memset(X,0,sizeof(X));
	memset(Y,0,sizeof(Y));
	for(int i=0;i<N*N;i++)
	   G[i].clear();
}
int Find(int u){
	for(int i=0;i<G[u].size();i++){
		int v=G[u][i];
		if(!vis[v]){
			vis[v]=1;
			if(match[v]==-1||Find(match[v])){
				match[v]=u;
				return 1;
			}
		}
	}
	return 0;
}
void Maxmatch(){
	memset(match,-1,sizeof(match));
	int ans=0;
	for(int i=1;i<x;i++){
		memset(vis,0,sizeof(vis));
		ans+=Find(i);
	}
	printf("%d\n",ans);
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		init();
		scanf("%d%d",&n,&m);
		for(int i=0;i<n;i++)
		   scanf("%s",mp[i]);
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(mp[i][j]=='*') X[i][j]=x;
				else if(mp[i][j]=='#') ++x;
			}
			++x;
		}
		for(int j=0;j<m;j++){
			for(int i=0;i<n;i++){
				if(mp[i][j]=='*') Y[i][j]=y;
				else if(mp[i][j]=='#') ++y;
			}
			++y;
		}
		for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
		   if(mp[i][j]=='*') G[X[i][j]].push_back(Y[i][j]);
		Maxmatch();
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/islittlehappy/article/details/81071311