Oil Skimming (Bipartite)

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

There's an oil tycoon going on a piece of NNOil is salvaged in the area, and its net can catch the area of ​​​​1 × 2

, #representing this place is oil, .representing this place is sea water, ask how much oil can be salvaged in this water area at most (oil and sea water cannot be fished together).

Number the oil first, the number is: 1 2 3 4 5... This kind of, then we traverse this area, if in 1 × 2There is oil in the area of ​​​​then, add an edge to their number, build a bipartite graph, and finally find the maximum matching of this bipartite graph is the answer.

Because the two-way edge is established, the final result needs to be divided by 2

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 650;
char mp [N] [N];
int num[N][N];
int match[N],vis[N];
int n,cnt;
int Next[4][2]={1,0,-1,0,0,1,0,-1};
vector<int>G[N];
void init(){
	memset(num,0,sizeof(num));
	cnt=0;
	for(int i=0;i<N;i++)
	   G[i].clear();
}
int Find (int u) {
	for(int i=0;i<G[u].size();i++){
		int v=G[u][i];
		if(!vis[v]){
			force[v]=1;
			if(match[v]==-1||Find(match[v])){
				match[v]=u;
				return 1;
			}
		}
	}
	return 0;
}
void Maxmatch(){
	memset(match,-1,sizeof(match));
	int ans=0;
	for(int i=1;i<=cnt;i++){
		memset(vis,0,sizeof(vis));
		ans+=Find(i);
	}
	printf("%d\n",ans/2);
}
int main(){
	int T,Cas=0;
	scanf("%d",&T);
	while(T--){
		init();
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%s",mp[i]);
			for(int j=0;j<n;j++){
				if(mp[i][j]=='#'){
					num[i][j]=++cnt;
				}
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(mp[i][j]=='#'){
					for(int k=0;k<4;k++){
						int x=i+Next[k][0];
						int y=j+Next[k][1];
						if(x<0||y<0||x>=n||y>=n) continue;
						if(mp[x][y]=='#')
						  G[num[i][j]].push_back(num[x][y]);
					}
				}
			}
		}
		printf("Case %d: ",++Cas);
		Maxmatch();
	}
	return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325900157&siteId=291194637