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
题意思路:求在图中最多有多少个互不重合的由两个相邻‘#’字符组成的1*2方块。很自然就想到了二分匹配,但是一开始的时候想错了,忽略了相同行或者列也可能出现重合,直接看成了http://acm.hdu.edu.cn/showproblem.php?pid=4619一道裸的二分匹配来做。所以这道题不能够直接分成两组来进行男女组合这样子的匹配。
解法:先对于每个'#'进行编号,然后找出每一个'#'上下左右能组合的'#'的编号。
这里用邻接表或者vector来装,再来套二分匹配的模板。但是这里每两个相邻的#是
双向记录的,所以最后的结果要除2。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<set>
#include<stack>
#include<vector>
#include<map>
#include<queue>
#define myself i,l,r
#define lson i<<1
#define rson i<<1|1
#define Lson i<<1,l,mid
#define Rson i<<1|1,mid+1,r
#define half (l+r)/2
#define inff 0x3f3f3f3f
#define lowbit(x) x&(-x)
#define me(a,b) memset(a,b,sizeof(a))
#define min4(a,b,c,d) min(min(a,b),min(c,d))
#define min3(x,y,z) min(min(x,y),min(y,z))
typedef long long ll;
using namespace std;
const int maxn=505;
char s[maxn][maxn];
int used[maxn*maxn],girl[maxn*maxn];
int head[maxn*maxn],sign,n;
int dir[4][2]={0,1,0,-1,1,0,-1,0};
int a[maxn][maxn];
struct node
{
int to,p;
}e[maxn];
void init()
{
sign=0;
me(a,0);
memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
e[sign]=node{v,head[u]};
head[u]=sign++;
}
bool check(int x,int y)
{
if(x<0||y<0||x>=n||y>=n||s[x][y]=='.')
return false;
return true;
}
int find(int x)
{
for(int i=head[x];i!=-1;i=e[i].p)
{
int r=e[i].to;
if(!used[r])
{
used[r]=1;
if(girl[r]==-1||find(girl[r]))
{
girl[r]=x;
return 1;
}
}
}
return 0;
}
void Creat()//找一找和每一个#上下左右相邻的#,然后存起来
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
if(s[i][j]=='.')
continue;
int u=a[i][j];
for(int k=0;k<4;k++)
{
int ii=i+dir[k][0];
int jj=j+dir[k][1];
if(check(ii,jj))
{
int v=a[ii][jj];
add(u,v),add(v,u);
}
}
}
}
int hungry(int n)
{
int ans=0;
memset(girl,-1,sizeof(girl));
for(int i=0;i<n;i++)
{
memset(used,0,sizeof(used));
if(find(i))
ans++;
}
return ans/2;//除二很容易理解,假设只有一对#相邻,这种方法记录两次边,就会得出最大匹配数为2
}
int main()
{
int T;
cin>>T;
for(int p=1;p<=T;p++)
{
int temp=0;
init();
cin>>n;
for(int i=0;i<n;i++)
{
scanf("%s",s[i]);
for(int j=0;j<n;j++)
{
if(s[i][j]=='#')
a[i][j]=temp++;//这里就是编号啦
}
}
Creat();
printf("Case %d: %d\n",p,hungry(temp));//hungry 就是二分匹配的模板
}
return 0;
}