forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
46 lines (46 loc) · 1.42 KB
/
Solution.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
class Solution {
public:
int minimumOperations(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<int> match(m * n, -1);
unordered_set<int> vis;
unordered_map<int, vector<int>> g;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if ((i + j) % 2 && grid[i][j]) {
int x = i * n + j;
if (i < m - 1 && grid[i + 1][j]) {
g[x].push_back(x + n);
}
if (i && grid[i - 1][j]) {
g[x].push_back(x - n);
}
if (j < n - 1 && grid[i][j + 1]) {
g[x].push_back(x + 1);
}
if (j && grid[i][j - 1]) {
g[x].push_back(x - 1);
}
}
}
}
int ans = 0;
function<int(int)> find = [&](int i) -> int {
for (int& j : g[i]) {
if (!vis.count(j)) {
vis.insert(j);
if (match[j] == -1 || find(match[j])) {
match[j] = i;
return 1;
}
}
}
return 0;
};
for (auto& [i, _] : g) {
ans += find(i);
vis.clear();
}
return ans;
}
};