forked from yogykwan/acm-challenge-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoj2226.cpp
109 lines (95 loc) · 2.32 KB
/
poj2226.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* POJ 2226: Muddy Fields
* 题意:给出一个矩阵,每个点是.或*。现在有一些宽为1长任意的木条,要用木条覆盖所有的*,但是保留所有的.。求最少使用的木条数量。
* 类型:二分匹配
* 算法:分为横竖考虑,将连续的*区域用一个木条表示,并编号。每个*都是一个横和一个竖交点。将横竖木条分列二分图两侧作为点,每个*作为边。则最小顶点覆盖数即为所求,二分图中其值等于最大匹配。
*/
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
vector<int> e[1300];
bool vis[1300];
int rec[1300];
int n1, n2;
bool Dfs(int u) {
for (vector<int>::iterator it = e[u].begin(); it != e[u].end(); ++it) {
int v = *it;
if (!vis[v]) {
vis[v] = true;
if (rec[v] == -1 || Dfs(rec[v])) {
rec[v] = u;
return true;
}
}
}
return false;
}
int Hungary() {
int ans = 0;
memset(rec, -1, sizeof(rec));
for (int i = 0; i < n1; ++i) {
memset(vis, 0, sizeof(vis));
if (Dfs(i)) ++ans;
}
return ans;
}
struct Line {
Line() {}
Line(int _x1, int _x2, int _y) : x1(_x1), x2(_x2), y(_y) {}
int x1, x2, y;
} heng[1300], shu[1300];
char mat[55][55];
int main() {
int r, c;
scanf("%d%d", &r, &c);
for (int i = 0; i < r; ++i) {
scanf("%s", mat[i]);
}
n1 = n2 = 0;
for (int i = 0; i < r; ++i) {
int x1 = -1;
for (int j = 0; j < c; ++j) {
if (mat[i][j] == '.') {
if (x1 != -1) {
heng[n1++] = Line(x1, j - 1, i);
x1 = -1;
}
} else {
if (x1 == -1) {
x1 = j;
}
}
}
if (x1 != -1) {
heng[n1++] = Line(x1, c - 1, i);
}
}
for (int i = 0; i < c; ++i) {
int x1 = -1;
for (int j = 0; j < r; ++j) {
if (mat[j][i] == '.') {
if (x1 != -1) {
shu[n2++] = Line(x1, j - 1, i);
x1 = -1;
}
} else {
if (x1 == -1) {
x1 = j;
}
}
}
if (x1 != -1) {
shu[n2++] = Line(x1, r - 1, i);
}
}
for (int i = 0; i < n1; ++i) {
for (int j = 0; j < n2; ++j) {
if (heng[i].y >= shu[j].x1 && heng[i].y <= shu[j].x2 && shu[j].y >= heng[i].x1 && shu[j].y <= heng[i].x2) {
e[i].push_back(j);
}
}
}
printf("%d\n", Hungary());
return 0;
}