题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4185
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
多亏了某"绿色"资源公司,出现了一个新的盈利行业的石油脱脂。墨西哥湾漂浮着大量的原油,正等着被有进取心的石油大亨们挖走。一个这样的油大亨有一个专机,可以掠过水面上集油的表面。然而,每勺覆盖一个10米×20米的矩形(去东/西或北/南)。它还要求矩形完全覆盖石油,否则产品被纯净的海水污染,因此无利可图!给定浮油的地图,油大亨希望您计算可提取的最大勺数。该地图是一个 NxN 网格,其中每个单元表示 10 米正方形的水,每个单元被标记为被油或纯净水覆盖。
输入
输入以整数 K (1 <= K <= 100) 开头,指示案例数。每个案例以整数 N (1 <= N <= 600) 开头,指示方形网格的大小。以下 N 行中的每一行都包含表示网格中一行单元格的 N 个字符。"*"的字符表示油性细胞,而"."字符表示纯水细胞。
输出
对于每种情况,应生产一行,格式如下:"案例 X:M",其中 X 是案例编号(从 1 开始),M 是可提取的最大勺油数。
这个题的难度在于建图。
怎样建图呢?其实我们可以这样想,对每一个#点进行编号,如果他能够通过上下左右移动得到另一个#点,那么这两个点的编号就连上一条边,题目要求的就是二分图的最大匹配,因为是无向图,所以不要忘记除2.
#include <bits/stdc++.h>
using namespace std;
int n,nx,ny;
int visited[6005],nxt[6005];
vector<int> G[6005];
char str[605][605];
int a[605][605],cnt;
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int find(int x){
for(int i = 0;i<G[x].size();++i){
int v=G[x][i];
if(!visited[v]){
visited[v]=1;
if(nxt[v]==-1||find(nxt[v])){
nxt[v]=x;
return 1;
}
}
}
return 0;
}
int match(){
int ans=0;
memset(nxt,-1,sizeof(nxt));
for(int i = 0;i<cnt;i++){
memset(visited,0,sizeof(visited));
if(find(i)) ans++;
}
return ans;
}
int main(int argc, char** argv) {
int K;
cin>>K;
for(int ncase=1;ncase<=K;++ncase){
cin>>n;
for(int i = 0;i<n;i++) scanf("%s",str[i]);
cnt=0;
for(int i = 0;i<n;++i){
for(int j = 0;j<n;++j){
if(str[i][j]=='#'){
a[i][j]=cnt++;
}
}
}
for(int i = 0;i<=6100;++i) G[i].clear();
for(int i = 0;i<n;++i){
for(int j = 0;j<n;++j){
if(str[i][j]=='.') continue;
int u=a[i][j];
for(int k = 0;k<4;k++){
int x=i+dir[k][0];
int y=j+dir[k][1];
if(x>=0&&y<n&&x<n&&y>=0&&str[x][y]=='#'){
int v=a[x][y];
G[u].push_back(v);
G[v].push_back(u);
}
}
}
}
printf("Case %d: %d\n",ncase,match()/2);
}
return 0;
}
有关二分图匹配的资料和知识可以参考我的这篇文章:https://blog.csdn.net/qq_43472263/article/details/96831025