Oil Skimming
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*10的,现有10*20的勺子可以提取出水上漂浮的油,问最多可以提取几勺的油;
每次提取的时候勺子放的位置都要是油,不然就被污染而没有价值了;
所以就是求最大匹配的;关键是建立边与边的关系,可以让有油的地方编号为1 2 3。。。然后再连接上下左右的点;
参考:https://www.cnblogs.com/zhengguiping--9876/p/4722349.html
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;
const int N = 666;
char mp[N][N];
int g[N][N];
int head[N], en;
struct Edge {
int v, to;
} edge[200010];
void addedge(int u, int v) {
edge[en].v = v;
edge[en].to = head[u];
head[u] = en++;
}
int link[N], in[N];
int dfs(int u) {
for (int i = head[u]; ~i; i = edge[i].to) {
int v = edge[i].v;
if (!in[v]) {
in[v] = 1;
if (!link[v] || dfs(link[v])) {
link[v] = u;
return 1;
}
}
}
return 0;
}
int main() {
int t, T = 1;
scanf ("%d", &t);
while (t--) {
int n;
scanf ("%d", &n);
for (int i = 1; i <= n; ++i){
scanf ("%s", mp[i]+1);
}
//建图
memset(g, 0, sizeof(g));
int k = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (mp[i][j] == '#') {
g[i][j] = ++k;
}
}
}
memset(head, -1, sizeof(head));
en = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (g[i][j]) {
int now = g[i][j];
int xia = g[i+1][j];
int you = g[i][j+1];
if (xia) {
addedge(now, xia);
addedge(xia, now);
}
if (you) {
addedge(now, you);
addedge(you, now);
}
}
}
}
int ans = 0;
memset(link, 0, sizeof(link));
for (int i = 1; i <= k; ++i) {
memset(in, 0, sizeof(in));
if (dfs(i)) ++ans;
}
printf ("Case %d: %d\n", T++, ans/2);
}
return 0;
}