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
 
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
", kase, ans);
79     }
80 }
View Code
原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7818324.html