-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode-NumberOfIslands.cpp
More file actions
85 lines (64 loc) · 1.84 KB
/
Copy pathLeetCode-NumberOfIslands.cpp
File metadata and controls
85 lines (64 loc) · 1.84 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
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
struct Graph {
unordered_map<int, unordered_set<int>> list;
int sz;
Graph(int sz) {
this->sz = sz;
}
void add_vertex(int u) {
list[u] = unordered_set<int>();
}
void add_edge(int u, int v) {
if (v <= 0 || v > sz) return;
if (list.count(v) <= 0) return;
list[u].insert(v);
list[v].insert(u);
}
};
int dfs(Graph& g) {
int components = 0;
unordered_set<int> visited;
for (int w = 1; w <= g.sz; ++w) {
if (visited.count(w) > 0) continue;
if (g.list.count(w) <= 0) continue;
++components;
stack<int> s;
s.push(w);
while (!s.empty()) {
int u = s.top();
s.pop();
if (visited.count(u) <= 0) {
visited.insert(u);
for (int v: g.list[u]) {
s.push(v);
}
}
}
}
return components;
}
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
if (grid.size() == 0) return 0;
int n = grid.size();
int m = grid[0].size();
int i = 1;
Graph g(n*m);
for (int r = 0; r < n; ++r) {
for (int c = 0; c < m; ++c, ++i) {
if (grid[r][c] == '1') g.add_vertex(i);
}
}
i = 1;
for (int r = 0; r < n; ++r) {
for (int c = 0; c < m; ++c, ++i) {
if (grid[r][c] == '0') continue;
if (c != 0) g.add_edge(i, i - 1);
if (c != m - 1) g.add_edge(i, i + 1);
g.add_edge(i, i + m);
g.add_edge(i, i - m);
}
}
return dfs(g);
}
};