forked from kaidul/LeetCode_problems_solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumber_of_Islands.cpp
36 lines (34 loc) · 951 Bytes
/
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
30
31
32
33
34
35
36
class Solution {
public:
int dx[4] = {0, -1, 0, 1};
int dy[4] = {-1, 0, 1, 0};
vector<vector<char> > grid;
int n, m;
int dfs(int x, int y) {
grid[x][y] = '0';
for(int i = 0; i < 4; ++i) {
int X = x + dx[i];
int Y = y + dy[i];
if(X >= 0 and Y >= 0 and X < n and Y < m and grid[X][Y] == '1') {
dfs(X, Y);
}
}
}
int numIslands(vector<vector<char>> &grid) {
this->grid = grid;
int result = 0;
n = grid.size();
if(n < 1) return result;
m = grid[0].size();
if(m < 1) return result;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
if(this->grid[i][j] == '1') {
result++;
dfs(i, j);
}
}
}
return result;
}
};