forked from kamyu104/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
number-of-islands.cpp
52 lines (47 loc) · 1.28 KB
/
number-of-islands.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
// Time: O(m * n)
// Space: O(m * n)
class Solution {
public:
/**
* @param grid a boolean 2D matrix
* @return an integer
*/
int numIslands(vector<vector<bool>>& grid) {
if (grid.empty()) {
return 0;
}
vector<vector<bool>> used(grid.size(),
vector<bool>(grid[0].size(), false));
int count = 0;
for (int i = 0; i < grid.size(); ++i) {
for (int j = 0; j < grid[0].size(); ++j) {
if (grid[i][j] && !used[i][j]) {
findIsland(grid, i, j, &used);
++count;
}
}
}
return count;
}
void findIsland(const vector<vector<bool>>& grid,
const int x,
const int y,
vector<vector<bool>> *used) {
if (!grid[x][y] || (*used)[x][y]) {
return;
}
(*used)[x][y] = true;
if (x > 0) {
findIsland(grid, x - 1, y, used);
}
if (x < grid.size() - 1) {
findIsland(grid, x + 1, y, used);
}
if (y > 0) {
findIsland(grid, x, y - 1, used);
}
if (y < grid[0].size() - 1) {
findIsland(grid, x, y + 1, used);
}
}
};