您的位置:首页 > 其它

hdu 4185 Oil Skimming【二分匹配】

2016-09-21 13:26 204 查看

Oil Skimming

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2432    Accepted Submission(s): 993


Problem Description

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

Source

The 2011 South Pacific Programming Contest

 

题目大意:

 给你一个n*n的矩阵,让你匹配出最多的“”##“”(横着竖着都行);

思路:

1、数据很水,直接建图跑二分匹配即可。

2、枚举所有#,与周围四个点如果是#的建立一条无向边,然后跑匈牙利二分匹配算法,输出匹配数/2即可,(因为我建立的是双向边)。

Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
char a[700][700];
int num[700][700];
int map[700][700];
int match[700];
int vis[700];
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};
int n,contn;
void add(int x,int y)
{
map[x][y]=1;
}
void getmap()
{
contn=0;
memset(num,0,sizeof(num));
memset(map,0,sizeof(map));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i][j]=='#')
{
num[i][j]=++contn;
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i][j]=='#')
{
for(int k=0;k<4;k++)
{
int x=i+fx[k];
int y=j+fy[k];
if(x>=1&&x<=n&&y>=1&&y<=n)
{
if(a[x][y]=='#')
{
add(num[i][j],num[x][y]);
}
}
}
}
}
}
}
int find(int u)
{
for(int i=1;i<=contn;i++)
{
if(map[u][i]==1&&vis[i]==0)
{
vis[i]=1;
if(match[i]==-1||find(match[i]))
{
match[i]=u;
return 1;
}
}
}
return 0;
}
void Slove()
{
int output=0;
memset(match,-1,sizeof(match));
for(int i=1;i<=contn;i++)
{
memset(vis,0,sizeof(vis));
if(find(i))output++;
}
printf("%d\n",output/2);
}
int main()
{
int t;
int kase=0;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",a[i]+1);
}
printf("Case %d: ",++kase);
getmap();
Slove();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdu 4185 杭电 4185