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
题目:HUD4185
题意:给你一把勺子,每次可以舀起两个相连的#,问在所给图中最多可以舀多少次。
思路:二分匹配,给图中每个#编号,相邻的两个#之间建边,我们就得到了一个二分图,答案即为最大匹配数。
AC代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define met(s,k) memset(s,k,sizeof s)
#define scan(a) scanf("%d",&a)
#define scanl(a) scanf("%lld",&a)
#define scann(a,b) scanf("%d%d",&a,&b)
#define scannl(a,b) scanf("%lld%lld",&a,&b)
#define scannn(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define prin(a) printf("%d\n",a)
#define prinl(a) printf("%lld\n",a)
using namespace std;
typedef long long ll;
const int inf=5005;
int k,n,m,vis[2*inf],match[2*inf],cont,pos[605][605],p[2*inf],num;
int mp[605][605];
struct edge
{
int st,en,next;
}e[10000];//邻接矩阵存边
void init()
{
met(p,-1);
num=0;
met(pos,0);
cont=1;
}
void add(int st,int en)
{
e[num].st=st;
e[num].en=en;
e[num].next=p[st];
p[st]=num++;
}
int matched(int u)
{
for(int i=p[u]; i!=-1; i=e[i].next)
{
int v=e[i].en;
if(!vis[v])
{
vis[v]=1;
if(!match[v]||matched(match[v]))
{
match[v]=u;
match[u]=v;
return 1;
}
}
}
return 0;
}
int hungarian()//匈牙利模板
{
int ans=0;
met(match,0);
for(int i=1; i<cont; i++)
{
if(!match[i])
{
met(vis,0);
if(matched(i))
{
ans++;
}
}
}
return ans;
}
int main()
{
int t,cas=0;
scan(t);
while(t--)
{
scan(n);
init();
for(int i=1; i<=n; i++)
{
getchar();
for(int j=1; j<=n; j++)
{
scanf("%c",&mp[i][j]);
if(mp[i][j]=='#')pos[i][j]=cont++;
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(pos[i][j])
{
if(pos[i+1][j])add(pos[i][j],pos[i+1][j]+inf);//建立一个虚拟点j+inf,便于匹配
if(pos[i-1][j])add(pos[i][j],pos[i-1][j]+inf);
if(pos[i][j+1])add(pos[i][j],pos[i][j+1]+inf);
if(pos[i][j-1])add(pos[i][j],pos[i][j-1]+inf);
}
}
}
printf("Case %d: %d\n",++cas,hungarian()/2);//因为匹配的是虚拟点,所以答案应该/2
}
return 0;
}