-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path200. Number of Islands_BFS.cc
39 lines (37 loc) · 1.36 KB
/
200. Number of Islands_BFS.cc
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
//Using DFS method
//TC: O(mn)
//SC: O(max(m, n)) //traverse layer by layer
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
int ans = 0;
queue<int> lands;
vector<pair<int,int>> dir{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int m = grid.size();
int n = grid[0].size();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == '1') {
ans++;
lands.push(i * n + j);
//Every element in the queue executes at most 4 times.
//If grid is filled with 1, then m * n elements will be in the queue.
while (lands.size() != 0) {
int index = lands.front();
lands.pop();
for (auto d : dir) {
int r = index / n + d.first;
int c = index % n + d.second;
if (r < 0 || r >= m || c < 0 || c >= n || grid[r][c] == '0') {
continue;
}
lands.push(r * n + c);
grid[r][c] = '0';
}
}
}
}
}
return ans;
}
};