2015
05-23

# 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.

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.

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.

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

Case 1: 3

KM二分匹配就不说了。

#include<stdio.h>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
int map[700][700],p[700][700],vis[700],x[700],y[700],t,n,num,k;
char s[700][700];
bool search(int i)
{
int j;
for(j=0;j<num;j++)
{
if(map[i][j]&&!vis[j])
{
vis[j]=1;
if(!y[j]||search(y[j]))
{
y[j]=i;
x[i]=j;
return true;
}
}
}
return false;
}
int match()
{
memset(x,0,sizeof(x));
memset(y,0,sizeof(y));
int ans=0;
for(int i=0;i<num;i++)
{
memset(vis,0,sizeof(vis));
if(search(i)) ans++;
}
return ans/2;//无向图，要除以2
}
int main()
{
scanf("%d",&t);
k=1;
while(t--)
{
int i,j;
num=0;
memset(map,0,sizeof(map));
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",s[i]);

for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(s[i][j]=='#')
p[i][j]=num++;

for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(s[i][j]=='#')
{
if(j<n-1&&s[i][j+1]=='#')
{
map[p[i][j]][p[i][j+1]]=1;
map[p[i][j+1]][p[i][j]]=1;
}
if(i<n-1&&s[i+1][j]=='#')
{
map[p[i][j]][p[i+1][j]]=1;
map[p[i+1][j]][p[i][j]]=1;
}
}
//map[i][j]='.';
}
int p=match();
printf("Case %d: %d\n",k++,p);
}
return 0;
}