-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path200. Number of Islands.cpp
44 lines (40 loc) · 1.03 KB
/
200. 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
37
38
39
40
41
42
43
44
class Solution {
public:
void dfs(int i , int j , vector<vector<char>>& grid )
{
if( i >= 0 && i < grid.size() && j>=0 && j<grid[0].size() && grid[i][j] =='1')
{
// cout<<"here";
grid[i][j] = '0';
dfs(i+1, j, grid);
dfs(i-1, j, grid);
dfs(i, j+1, grid);
dfs(i, j-1, grid);
}
}
int numIslands(vector<vector<char>>& grid){
int m = grid.size();
int n = grid[0].size();
int count = 0;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if( grid[i][j] == '1')
{
count++;
dfs(i,j,grid);
}
}
}
// for (int i = 0; i < m; i++)
// {
// for (int j = 0; j < n; j++)
// {
// cout<<grid[i][j];
// }
// cout<<endl;
// }
return count;
}
};