-
Notifications
You must be signed in to change notification settings - Fork 3
/
200.cpp
28 lines (28 loc) · 867 Bytes
/
200.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
class Solution {
public:
vector<pair<int, int>> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
void dfs(vector<vector<char>>& grid, int i, int j) {
if (grid[i][j] == '0') return;
grid[i][j] = '0';
for (auto& direction : directions) {
int x = i + direction.first;
int y = j + direction.second;
if (x < 0 || x >= grid.size() || y < 0 || y >= grid[0].size()) continue;
dfs(grid, x, y);
}
}
int numIslands(vector<vector<char>>& grid) {
int m = grid.size();
int n = grid[0].size();
int res = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == '1') {
res++;
dfs(grid, i, j);
}
}
}
return res;
}
};