Oil Skimming

Thanks to a certain "green" resources company, there is a new profitable industry of oil skimming. There are large slicks of crude oil floating in the Gulf of Mexico just waiting to be scooped up by enterprising oil barons. One such oil baron has a special plane that can skim the surface of the water collecting oil on the water's surface. However, each scoop covers a 10m by 20m rectangle (going either east/west or north/south). It also requires that the rectangle be completely covered in oil, otherwise the product is contaminated by pure ocean water and thus unprofitable! Given a map of an oil slick, the oil baron would like you to compute the maximum number of scoops that may be extracted. The map is an NxN grid where each cell represents a 10m square of water, and each cell is marked as either being covered in oil or pure water.

Input

The input starts with an integer K (1 <= K <= 100) indicating the number of cases. Each case starts with an integer N (1 <= N <= 600) indicating the size of the square grid. Each of the following N lines contains N characters that represent the cells of a row in the grid. A character of '#' represents an oily cell, and a character of '.' represents a pure water cell.

Output

For each case, one line should be produced, formatted exactly as follows: "Case X: M" where X is the case number (starting from 1) and M is the maximum number of scoops of oil that may be extracted.

Sample Input

1
6
......
.##...
.##...
....#.
....##
......

Sample Output

Case 1: 3

题意:#代表石油,.代表海水,每格为10*10的正方形,有一家公司由于技术改进,可以收集海上的石油,每次收集为宽10长20(或宽20长10)的矩形,而且收集的还不能有海水,问最多能收集几次

思路:对每个位置上的石油进行编号,x数组记录石油编号的行,y数组记录石油编号的列,

然后两层for循环遍历,如果两块石油在海上的位置相邻(即上下左右相邻),用a数组标记,

最后二分图最大匹配遍历一遍所有石油,得出的结果/2(因为建的是双向边),即为所求。

注意:数组开的大小,容易出各种错误

#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int match[11010],f[11101];
int b[1101][1101],n,k;
int x[11011],y[11001];
char a[601][601];
int dfs(int x)
{
	for(int i=1;i<k;i++)
	if(!f[i]&&b[x][i])
	{
		f[i]=1;
		if(match[i]==0||dfs(match[i]))
		{
			match[i]=x;
			return 1;
		}
	}
	return 0;
}
int main()
{
	int i,j,w,zz=0;
	int n,m;
	cin>>w;
	while(w--)
	{
		k=1;
		cin>>n;
		for(i=0; i<n; i++)
		{
			cin>>a[i];
			for(j=0;j<n;j++)
			if(a[i][j]=='#')
			{
				x[k]=i;
				y[k++]=j;
			}
		}	
		memset(b,0,sizeof(b));
		for(i=1; i<k; i++)
		{
			for(j=1; j<k; j++)
			{
				int t=abs(x[i]-x[j])+abs(y[i]-y[j]);
                //判断是否相邻
				if(t==1)
					b[i][j]=1;
			}
		}
		int s=0;
		memset(match,0,sizeof(int)*(k+1));
		for(i=1;i<k;i++)
		{
			memset(f,0,sizeof(int)*(k+1));
			if(dfs(i))
			s++;
		}
		printf("Case %d: %d\n",++zz,s/2);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_52898168/article/details/119255989
今日推荐