-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode200.cpp
More file actions
34 lines (34 loc) · 858 Bytes
/
LeetCode200.cpp
File metadata and controls
34 lines (34 loc) · 858 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
28
29
30
31
32
33
34
class Solution
{
public:
void dfs(int i, int j, vector<vector<char>> &grid)
{
if (i < 0 or j < 0 or i >= grid.size() or j >= grid[0].size() or grid[i][j] == '0' or grid[i][j] == '2')
return;
grid[i][j] = '2';
dfs(i + 1, j, grid);
dfs(i - 1, j, grid);
dfs(i, j + 1, grid);
dfs(i, j - 1, grid);
return;
}
int numIslands(vector<vector<char>> &grid)
{
int row = grid.size();
int col = grid[0].size();
int cnt = 0;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (grid[i][j] == '1')
{
dfs(i, j, grid);
cnt++;
cout << cnt << endl;
}
}
}
return cnt;
}
};