-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path200.cpp
More file actions
31 lines (29 loc) · 1.08 KB
/
200.cpp
File metadata and controls
31 lines (29 loc) · 1.08 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
class Solution {
public:
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
int numIslands(vector<vector<char>>& grid) { //O(NM)
int g = 0;
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'){
g++;
queue<pair<int,int>> q; q.push({i,j}); grid[i][j] = '0';
while(!q.empty()){
pair<int,int> p = q.front(); q.pop();
int i_ = p.first; int j_ = p.second;
for(int d = 0; d < 4; d++){
int newi = i_+dx[d]; int newj = j_+dy[d];
if(newi >= 0 && newi < m && newj >= 0 && newj < n && grid[newi][newj] == '1'){
grid[newi][newj] = '0';
q.push({newi,newj});
}
}
}
}
}
}
return g;
}
};