HDU 5093 Battle ships(二分图+建图)

                                            Battle ships

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1877    Accepted Submission(s): 695


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

题目大意:在'*'号上放置战舰,一行或一列上都只能有一个战舰,除非有冰山('#')在中间挡着,那么冰山两边就可以各放置一个战舰,问最多放多少战舰

二分图匹配,只要建好图就行了

对于能放置战舰的地方,我们进行行标号和列,如果有冰山隔着或者是变到了下一行,标号+1,然后将行列相连,跑匈牙利即可

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=2510;
int n,m;
int x_x,y_y;
char str[maxn][maxn];
int x[maxn][maxn];
int y[maxn][maxn];
bool vis[maxn];
int match[maxn];
int map[2510][2510];
void getX()
{
	x_x=1;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(str[i][j]=='*')
			{
				x[i][j]=x_x;
			}
			else if(str[i][j]=='#')
			{
				x_x++;
			}
		}
		x_x++;
	}
	return;
}
void getY()
{
	y_y=1;
	for(int j=1;j<=n;j++)
	{
		for(int i=1;i<=m;i++)
		{
			if(str[i][j]=='*')
			{
				y[i][j]=y_y;
			}
			else if(str[i][j]=='#')
			{
				y_y++;
			}
		}
		y_y++;
	}
	return;
}
bool dfs(int node)
{
	for(int i=1;i<y_y;i++)
	{
		if(!vis[i]&&map[node][i])
		{
			vis[i]=true;
			if(match[i]==-1||dfs(match[i]))
			{
				match[i]=node;
				return true;
			}
		}
	}
	return false;
}
int hungry()
{
	int ans=0;
	memset(match,-1,sizeof(match));
	for(int i=1;i<x_x;i++)
	{
		memset(vis,false,sizeof(vis));
		if(dfs(i))
		{
			ans++;
		}
	}
	return ans;
}
int main()
{
	int test;
	scanf("%d",&test);
	while(test--)
	{
		memset(map,0,sizeof(map));
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
		{
			scanf("%s",str[i]+1);
		}
		getX();

		getY();
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				if(str[i][j]=='*')
				{
					map[x[i][j]][y[i][j]]=1;
				}
			}
		}
		cout<<hungry()<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37943488/article/details/81184104