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
题意: 一次挖两个相邻(上下左右)石油(两个#),问提取的最大油勺数。
思路: 看着题,很容易想到二分匹配,但是如何匹配呢,就需要建图了,把石油编号,遇到石油就四面搜索,把能匹配的记录下来,然后就是二分匹配的模板了
AC代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[1100][1100],book[1100],match[1100],n,e[1100][1100];
char b[1100][1100];
int d[4][2]= {0,1,1,0,-1,0,0,-1},s;
int dfs(int x)
{
for(int i=1; i<s; i++)
{
if(e[x][i]&&!book[i])
{
book[i]=1;
if(!match[i]||dfs(match[i]))
{
match[i]=x;
return 1;
}
}
}
return 0;
}
int main()
{
int k,i,j,t=1;
scanf("%d",&k);
while(k--)
{
memset(a,0,sizeof(a));
memset(e,0,sizeof(e));
memset(match,0,sizeof(match));
s=1;
scanf("%d",&n);
for(i=0; i<n; i++)
{
scanf("%s",b[i]);
for(j=0; j<n; j++)
{
if(b[i][j]=='#')
a[i][j]=s++;//编号
}
}
int xx,yy;
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
if(a[i][j])//搜索能匹配的石油
{
for(int kk=0; kk<4; kk++)
{
xx=i+d[kk][0];
yy=j+d[kk][1];
if(xx<0||yy<0||xx>=n||yy>=n)
continue;
if(b[xx][yy]=='#')
{
e[a[i][j]][a[xx][yy]]=1;//双向
e[a[xx][yy]][a[i][j]]=1;
}
}
a[i][j]=0;//取消标记避免重复
}
}
}
int ans=0;
for(i=1; i<s; i++)
{
memset(book,0,sizeof(book));
if(dfs(i))
ans++;
}
// for(i=1;i<s;i++)
// printf("%d %d*****\n",i,match[i]);
printf("Case %d: %d\n",t++,ans/2);
}
return 0;
}
本文探讨了一种创新的算法,利用二分匹配原理解决油污清理问题。通过在地图上标记石油和水区域,设计一种策略计算在保持纯度要求下,最大化可提取的石油勺数。通过建图和搜索邻接石油资源,实现高效的油污回收方案。
&spm=1001.2101.3001.5002&articleId=119280236&d=1&t=3&u=701b4f90f29e43eb86ffbfbf64ae7187)
1051

被折叠的 条评论
为什么被折叠?



