-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0200.cpp
More file actions
27 lines (27 loc) · 925 Bytes
/
Copy path0200.cpp
File metadata and controls
27 lines (27 loc) · 925 Bytes
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
class Solution {
public:
int numIslands(vector<vector<char>> &grid) {
queue<pair<int, int>> q;
int cnt = 0;
int m = grid.size(), n = grid[0].size();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == '0') continue;
q.push(make_pair(i, j));
cnt++;
while (!q.empty()) {
int i = q.front().first;
int j = q.front().second;
q.pop();
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == '0') continue;
grid[i][j] = '0';
q.push(make_pair(i + 1, j));
q.push(make_pair(i - 1, j));
q.push(make_pair(i, j + 1));
q.push(make_pair(i, j - 1));
}
}
}
return cnt;
}
};