-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path947.cpp
More file actions
38 lines (37 loc) · 1.22 KB
/
Copy path947.cpp
File metadata and controls
38 lines (37 loc) · 1.22 KB
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
class Solution {
public:
int removeStones(vector<vector<int>> &stones) {
int total = 0;
unordered_map<int, unordered_set<int>> row, col;
for (auto &s:stones) {
if (row.find(s[0]) == row.end())
row.insert(pair<int, unordered_set<int>>(s[0], unordered_set<int>()));
row[s[0]].insert(s[1]);
if (col.find(s[1]) == col.end())
col.insert(pair<int, unordered_set<int>>(s[1], unordered_set<int>()));
col[s[1]].insert(s[0]);
}
for (auto &s:stones) {
if (!row[s[0]].empty())
total += DFS(stones, s[0], s[1], row, col) - 1;
}
return total;
}
int DFS(vector<vector<int>> &stone, int x, int y, unordered_map<int, unordered_set<int>> &row,
unordered_map<int, unordered_set<int>> &col) {
int sum = 1;
row[x].erase(y);
col[y].erase(x);
for (auto r:row[x]) {
sum += DFS(stone, x, r, row, col);
if (row[x].empty())
break;
}
for (auto c:col[y]) {
sum += DFS(stone, c, y, row, col);
if (col[y].empty())
break;
}
return sum;
}
};