hdu 4185 Oil Skimming(二分图匹配 经典建图+匈牙利模板)

时间:2023-01-25 09:27:15
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 ( <= K <= ) indicating the number of cases. Each case starts with an integer N ( <= N <= ) 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 ) and M is the maximum number of scoops of oil that may be extracted.
Sample Input

......
.##...
.##...
....#.
....##
......
Sample Output
Case : 
Source

题意:求能够找到最多多少个“##”(横或竖都行)

解题:关键是建图,在建图时,先用tmp[][]数组将‘#’的数字存起来(tmp=0开始),然后再遍历一遍,如果遇到的是mp[i][j]=='#',则将上下左右是‘#’的标记为cnt[ tmp[][] ][tmp[][] ]=1,最后就是经典的匈牙利算法了。

       犯了个致命的错误,在匈牙利算法时所对应的n应该是num,即重新建图后的‘n’。

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 606
#define inf 1e12
int n;
char mp[N][N];
int tmp[N][N];
int cnt[N][N];
int match[N];
int vis[N];
int tot;
bool dfs(int u){
for(int i=;i<tot;i++){
if(!vis[i] && cnt[u][i]){
vis[i]=;
if(match[i]==- || dfs(match[i])){
match[i]=u;
return true;
}
}
}
return false;
} int solve(){
int ans=;
memset(match,-,sizeof(match));
for(int i=;i<tot;i++){
memset(vis,,sizeof(vis));
if(dfs(i)){
ans++;
}
} return ans;
}
int main()
{
int t,ac=;;
scanf("%d",&t);
while(t--){
memset(tmp,,sizeof(tmp));
scanf("%d",&n);
int num=;
for(int i=;i<n;i++){
scanf("%s",mp[i]);
for(int j=;j<n;j++){
if(mp[i][j]=='#'){
tmp[i][j]=num++;
}
}
}
tot=num;
memset(cnt,,sizeof(cnt));
for(int i=;i<n;i++){
for(int j=;j<n;j++){
if(mp[i][j]!='#') continue;
if(i> && mp[i-][j]=='#') cnt[tmp[i][j]][tmp[i-][j]]=;
if(i<n- && mp[i+][j]=='#') cnt[tmp[i][j]][tmp[i+][j]]=;
if(j> && mp[i][j-]=='#') cnt[tmp[i][j]][tmp[i][j-]]=;
if(j<n- && mp[i][j+]=='#') cnt[tmp[i][j]][tmp[i][j+]]=;
}
}
printf("Case %d: ",++ac);
int ans=solve();
printf("%d\n",ans/);
}
return ;
}