Oil Skimming
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3048 Accepted Submission(s): 1274
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
题意:给出一个图,#为油,每次可以打捞长为2宽为1的图,不可重复打捞,问最多需要打捞几次
分析:由于每次都是打捞相邻的两个,而且只能用一次,那么就可以用二部图最大匹配解决这个问题
将所有的#存在结构体中,找出所有与自身相邻的#建边,为保证每个点只能用一次,这里还要双向建边
被单双向建边搞懵了,那么什么时候双向建边,什么时候单向?
1.当二部图左边和右边的含义相同时,那么需要双向建边
比如本题,二部图的左边和右边都是#,他们的含义相同,如果u和v匹配,那么v就不能再使用,双向建边的话,可以让v
匹配一个u,或者其他的一个不影响答案的无关点
2.当二部图左右两边含义不同时,单向建边
比如
hdu1083这题,二部图的两边含义是不同的,左边表示课程,右边表示学生,此时如果v被用,那么是学生集合里的v被用
而不影响课程里的v
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N=605;
int link[N*N],n,num;
char e[N][N];
bool vis[N*N];
int go[4][2]= {1,0,0,1,-1,0,0,-1};
vector<int>g[N*N];
struct node
{
int x,y;
} mp[N*N];
int find(int u)
{
for(int i=0; i<g[u].size(); i++)
{
if(!vis[g[u][i]])
{
vis[g[u][i]]=1;
if(link[g[u][i]]==-1||find(link[g[u][i]]))
{
link[g[u][i]]=u;
return 1;
}
}
}
return 0;
}
int matching()
{
mem(link,-1);
int ans=0;
for(int i=0; i<num; i++)
{
mem(vis,0);
ans+=find(i);
}
return ans;
}
int main()
{
int t,q=0;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
num=0;
for(int i=1; i<=n; i++)
{
scanf("%s",e[i]+1);
for(int j=1; j<=n; j++)
{
if(e[i][j]=='#')
{
mp[num].x=i;
mp[num++].y=j;
}
}
}
for(int i=0;i<num;i++)
{
for(int j=0;j<num;j++)
{
bool flag=false;
for(int k=0;k<4;k++)
{
int tx=mp[i].x+go[k][0];
int ty=mp[i].y+go[k][1];
if(tx==mp[j].x&&ty==mp[j].y)
{
flag=true;
break;
}
}
if(flag)g[i].push_back(j);
}
}
printf("Case %d: %d\n",++q,matching()/2);
for(int i=0;i<num;i++)
g[i].clear();
}
return 0;
}