-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Number of Islands.cpp
58 lines (41 loc) · 1.63 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Solution {
void mark_current_island(vector<vector<char>> &matrix,int x,int y,int r,int c)
//Here int x and int y is ->> Current cordinate
//Here int r and int c is ->> grid (m * n) size
{
if(x<0 || x>=r || y<0 || y>=c || matrix [x][y] != '1') //Boundary case
return;
//Make current call as visited
matrix [x][y] = '2';
//Make recursive call in all adjacent directions
mark_current_island(matrix,x-1,y,r,c); //Top
mark_current_island(matrix,x+1,y,r,c); //Down
mark_current_island(matrix,x,y-1,r,c); //Left
mark_current_island(matrix,x,y+1,r,c); //right
}
public:
int numIslands(vector<vector<char>>& grid) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int rows = grid.size();
if(rows == 0) //Empty Grid Boundary Case
{
return 0;
}
int cols = grid[0].size();
//Now iterate all cols of the array
int No_of_Island = 0;
for(int i=0;i<rows;++i)
{
for(int j=0;j<cols;++j)
{
if(grid[i][j] =='1')
{
mark_current_island (grid,i,j,rows,cols);
No_of_Island += 1;
}
}
}
return No_of_Island;
}
};