-
Notifications
You must be signed in to change notification settings - Fork 0
/
200. Number of Islands.cpp
29 lines (29 loc) · 1.04 KB
/
200. 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
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
int m = grid.size(), n, islands = 0, dir[] = {0, 1, 0, -1, 0};
if(m > 0) n = grid[0].size(); else n = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == '1') {
islands++;
grid[i][j] = '0';
queue<pair<int, int>> q;
q.push({i, j});
while (!q.empty()) {
pair<int, int> p = q.front();
q.pop();
for (int k = 0; k < 4; k++) {
int r = p.first + dir[k], c = p.second + dir[k + 1];
if (r >= 0 && r < m && c >= 0 && c < n && grid[r][c] == '1') {
grid[r][c] = '0';
q.push({r, c});
}
}
}
}
}
}
return islands;
}
};