HDU - 4185 —— Oil Skimming —— 二分匹配
2017-04-23 16:54
399 查看
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.
InputThe 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.
OutputFor 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
Sample Output
这道题目是说给你一个规模为n的方阵,里面的‘#’号是指油田,'.'是指水。当有10*20大小的矩形存在的时候,表示才能够提取出油来。
问所给的矩阵中符合条件的情况中最大能提取出多少油来。
思路:二分匹配+dfs。先遍历所有的字符。如果是‘#’,就标记坐标和个数,,记录个数最后直接dfs,降低复杂度。
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#define MAX_N 100005
#define INF 0x3f3f3f3f
#define Mem(a,x) memset(a,x,sizeof(a))
#define ll long long
char m[605][605];
int mp[605][605],gra[605][605],link[10005];
bool used[360005];
int N;
bool dfs(int u) {
for(int v = 0; v<N; v++) {
if(!gra[u][v] || used[v]) continue;
used[v] = true;
if(link[v] == -1 || dfs(link[v])) {
link[v] = u;
return true;
}
}
return false;
}
int solve() {
Mem(link,-1);
int sum = 0;
for(int i = 0; i<N; i++) {
Mem(used,false);
used[i] = true;
if(dfs(i)) sum ++;
}
return sum;
}
int main()
{
int k;
cin>>k;
int num = 1;
while(k--) {
int n;
cin>>n;
N = 0;
Mem(mp,0);
Mem(gra,0);
for(int i = 0; i<n; i++) {
scanf("%s",&m[i]);
}
for(int i = 0; i<n; i++) {
for(int j = 0; j<n; j++) {
if(m[i][j] == '#') {
mp[i][j] = N++;
}
}
}
for(int i = 0; i<n; i++) {
for(int j = 0; j<n; j++) {
if(m[i][j] == '.') continue;
if(i-1>=0 && m[i-1][j] == '#') gra[mp[i][j]][mp[i-1][j]] = 1;
if(i+1<n && m[i+1][j] == '#') gra[mp[i][j]][mp[i+1][j]] = 1;
if(j-1>=0 && m[i][j-1] == '#') gra[mp[i][j]][mp[i][j-1]] = 1;
if(j+1<n && m[i][j+1] == '#') gra[mp[i][j]][mp[i][j+1]] = 1;
}
}
int ans = solve() / 2;
printf("Case %d: %d\n",num ++,ans);
}
return 0;
}
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.
InputThe 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.
OutputFor 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
这道题目是说给你一个规模为n的方阵,里面的‘#’号是指油田,'.'是指水。当有10*20大小的矩形存在的时候,表示才能够提取出油来。
问所给的矩阵中符合条件的情况中最大能提取出多少油来。
思路:二分匹配+dfs。先遍历所有的字符。如果是‘#’,就标记坐标和个数,,记录个数最后直接dfs,降低复杂度。
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#define MAX_N 100005
#define INF 0x3f3f3f3f
#define Mem(a,x) memset(a,x,sizeof(a))
#define ll long long
char m[605][605];
int mp[605][605],gra[605][605],link[10005];
bool used[360005];
int N;
bool dfs(int u) {
for(int v = 0; v<N; v++) {
if(!gra[u][v] || used[v]) continue;
used[v] = true;
if(link[v] == -1 || dfs(link[v])) {
link[v] = u;
return true;
}
}
return false;
}
int solve() {
Mem(link,-1);
int sum = 0;
for(int i = 0; i<N; i++) {
Mem(used,false);
used[i] = true;
if(dfs(i)) sum ++;
}
return sum;
}
int main()
{
int k;
cin>>k;
int num = 1;
while(k--) {
int n;
cin>>n;
N = 0;
Mem(mp,0);
Mem(gra,0);
for(int i = 0; i<n; i++) {
scanf("%s",&m[i]);
}
for(int i = 0; i<n; i++) {
for(int j = 0; j<n; j++) {
if(m[i][j] == '#') {
mp[i][j] = N++;
}
}
}
for(int i = 0; i<n; i++) {
for(int j = 0; j<n; j++) {
if(m[i][j] == '.') continue;
if(i-1>=0 && m[i-1][j] == '#') gra[mp[i][j]][mp[i-1][j]] = 1;
if(i+1<n && m[i+1][j] == '#') gra[mp[i][j]][mp[i+1][j]] = 1;
if(j-1>=0 && m[i][j-1] == '#') gra[mp[i][j]][mp[i][j-1]] = 1;
if(j+1<n && m[i][j+1] == '#') gra[mp[i][j]][mp[i][j+1]] = 1;
}
}
int ans = solve() / 2;
printf("Case %d: %d\n",num ++,ans);
}
return 0;
}
相关文章推荐
- HDU 4185 — Oil Skimming 二分匹配
- HDU-4185-Oil Skimming [二分匹配]
- HDU 4185 Oil Skimming(二分匹配,匈牙利算法)
- hdu 4185 Oil Skimming (二分匹配)
- 【二分匹配】hdu 4185 Oil Skimming
- hdu 4185 Oil Skimming 二分匹配
- HDU 4185 Oil Skimming ( 二分匹配建图 )
- HDU 4185 Oil Skimming (二分匹配)
- hdu 4185 Oil Skimming【二分匹配】
- HDU 4185 二分匹配 1*2方格个数
- hdu 4185 Oil Skimming (最大匹配)
- HDU 4185 Oil Skimming(二分图最大匹配)
- HDU 4185 Oil Skimming(二分图最大匹配)
- HDU 4185 Oil Skimming 【离散化二分匹配 黑白染色】
- hdu 4185 (Oil Skimming) 奇偶匹配
- hdu 4185 Oil Skimming(最大匹配)
- HDU 4185 Oil Skimming (最大匹配)
- HDU 4185 Oil Skimming(二分图最大匹配)
- hdu 4185 Oil Skimming 【最大匹配】
- hdu 4185 Oil Skimming(最大匹配)