forked from Pheonix-001/Hacktoberfest_2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumber_Of_islands.cpp
44 lines (28 loc) · 864 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
37
38
39
40
41
42
43
44
// IT IS THE PROGRAM TO COUNT THE NUMBER OF ISLANDS
// WHICH IS BASICALLY THE GROUP OF 1'S IN THE MATRICES
// SURROUNDED BY THE WATER RPRESENTED BY 0'S
#include<bits/stdc++.h>
using namespace std;
void solve(vector<vector<char>>&grid,int n,int m,int i,int j){
if((i<n)&&(j<m)&&(i>=0)&&(j>=0)&&(grid[i][j]=='1')){
grid[i][j] = '0';
solve(grid,n,m,i+1,j);
solve(grid,n,m,i-1,j);
solve(grid,n,m,i,j+1);
solve(grid,n,m,i,j-1);
}
}
int numIslands(vector<vector<char>>& grid) {
int n = grid.size();
int m = grid[0].size();
int count = 0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(grid[i][j]=='1'){
solve(grid,n,m,i,j);
count++;
}
}
}
return count;
}