HDU4185 Oil Skimming —— 最大匹配

题目链接:https://vjudge.net/problem/HDU-4185

Oil Skimming

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3016    Accepted Submission(s): 1262

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 (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

Source

The 2011 South Pacific Programming Contest

Recommend

lcy

题解:

1.首先为每个油田编号。然后对于当前的油田, 如果它的上面有油田,则在这两个油田之间连一条边,同理其他三个方向。

2.利用匈牙利算法求出最大匹配数,即为答案。

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <string>
 6 #include <vector>
 7 #include <map>
 8 #include <set>
 9 #include <queue>
10 #include <sstream>
11 #include <algorithm>
12 using namespace std;
13 const int INF = 2e9;
14 const int MOD = 1e9+7;
15 const int MAXN = 600+10;
16
17 int n, N;
18 char a[MAXN][MAXN];
19 int M[MAXN][MAXN], id[MAXN][MAXN], link[MAXN];
20 bool vis[MAXN];
21
22 bool dfs(int u)
23 {
24     for(int i = 1; i<=N; i++)
25     if(M[u][i] && !vis[i])
26     {
27         vis[i] = true;
28         if(link[i]==-1 || dfs(link[i]))
29         {
30             link[i] = u;
31             return true;
32         }
33     }
34     return false;
35 }
36
37 int hungary()
38 {
39     int ret = 0;
40     memset(link, -1, sizeof(link));
41     for(int i = 1; i<=N; i++)
42     {
43         memset(vis, 0, sizeof(vis));
44         if(dfs(i)) ret++;
45     }
46     return ret;
47 }
48
49 int main()
50 {
51     int T;
52     scanf("%d", &T);
53     for(int kase = 1; kase<=T; kase++)
54     {
55         scanf("%d", &n);
56         N = 0;
57         memset(id, -1, sizeof(id));
58         for(int i = 1; i<=n; i++)
59         {
60             scanf("%s", a[i]+1);
61             for(int j = 1; j<=n; j++)   //为每个油田编号
62                 if(a[i][j]==‘#‘)
63                     id[i][j] = ++N;
64         }
65
66         memset(M, false, sizeof(M));
67         for(int i = 1; i<=n; i++)
68         for(int j = 1; j<=n; j++)
69         {
70             if(id[i][j]==-1) continue;
71             if(j!=1 && id[i][j-1]!=-1) M[id[i][j]][id[i][j-1]] = true;
72             if(j!=n && id[i][j+1]!=-1) M[id[i][j]][id[i][j+1]] = true;
73             if(i!=1 && id[i-1][j]!=-1) M[id[i][j]][id[i-1][j]] = true;
74             if(i!=n && id[i+1][j]!=-1) M[id[i][j]][id[i+1][j]] = true;
75         }
76
77         int ans = hungary()/2;
78         printf("Case %d: %d\n", kase, ans);
79     }
80 }

时间: 2024-10-03 05:40:19

HDU4185 Oil Skimming —— 最大匹配的相关文章

匈牙利算法求最大匹配(HDU-4185 Oil Skimming)

如下图:要求最多可以凑成多少对对象 ? 大佬博客:https://blog.csdn.net/cillyb/article/details/55511666 模板: int link[maxn],vis[maxn]; bool dfs(int x) { for(int i = 1; i <= num; i++) { if(!vis[i] && cp[x][i]) { vis[i] = 1; if(link[i] == 0 || dfs(link[i])) { link[i] = x;

hdu4185 Oil Skimming

要用1×2的板子尽量多的覆盖##区域,且不能交叉,求至多可以覆盖多少板子. 每一个#向向下或向右相邻的#建边.求最大匹配就可以了. 其实这题数据是比较弱的把,应该是#的个数在600以内把.. #include <iostream> #include <cstdlib> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <

hdu4185 Oil Skimming(二分匹配)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int n,cot; int map[660],vis[660],pp[660][660],u[660][660]; int bfs(int x) { for(int i=1;i<=cot;i++) { if(!vis[i]&&pp[x][i]) { vis[i]=1; if(!ma

4185 Oil Skimming 最大匹配 奇偶建图

题目大意: 统计相邻(上下左右)的‘#’的对数. 解法: 与题目hdu1507 Uncle Tom's Inherited Land*类似,需要用奇偶建图.就是行+列为奇数的作为X集合,偶尔作为Y集合,都是‘#’就连边.最后求最大匹配. 数据有点大,直接建图会出错(我试过).可以按照‘#’出现的顺序给顶点编号. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue&

HDU4185 Oil Skimming(匈牙利)

题意: n*n的图 相邻两##可除去 问最多除去多少 奇偶建图,OK /* *********************************************** //Author :devil //Created Time :2016/5/12 14:48:15 //************************************************ */ #include <cstdio> #include <cstring> #include <io

hdu4185+poj3020(最大匹配+最小边覆盖)

传送门:hdu4185 Oil Skimming 题意:n*n的方格里有字符*和#,只能在字符#上放1*2的板子且不能相交,求最多能放多少个. 分析:直接给#字符编号,然后相邻的可以匹配,建边后无向图跑匈牙利算法,最后得到的最大匹配数/2. #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <iostream> #include <

HDU 4185 ——Oil Skimming——————【最大匹配、方格的奇偶性建图】

Oil Skimming Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4185 Description Thanks to a certain "green" resources company, there is a new profitable industry of oil skimming. There ar

J - Oil Skimming 二分图的最大匹配

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

hdu 4185 Oil Skimming(二分匹配)

Oil Skimming Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 883    Accepted Submission(s): 374 Problem Description Thanks to a certain "green" resources company, there is a new profitable