forked from bamsarts/DS-ALGO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo_of_Islands.cpp
38 lines (31 loc) · 922 Bytes
/
No_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
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
int n=grid.size();
if(n==0)
return 0;
int m=grid[0].size();
if(m==0)
return 0;
int island_count = 0;
for(int i=0;i<grid.size();i++){
for(int j=0;j<grid[i].size();j++){
if(grid[i][j] == '1')
{
island_count += Islandutil(grid,i,j);
}
}
}
return island_count;
}
int Islandutil(vector<vector<char>>& grid, int i, int j) {
if ( i<0 || i>=grid.size() || j<0 || j>=grid[i].size() || grid[i][j] == '0')
return 0;
grid[i][j] = '0';
Islandutil(grid,i+1,j);
Islandutil(grid,i-1,j);
Islandutil(grid,i,j+1);
Islandutil(grid,i,j-1);
return 1;
}
};