forked from MAYANK25402/Hacktober-Fest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(GFG POTD) Number Of Islands
75 lines (75 loc) · 1.64 KB
/
(GFG POTD) Number Of Islands
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class Solution {
public:
vector<vector<int>>grid;
int n,m;
void dfs(vector<vector<int>>&visited,int i,int j)
{
if(i<0 || j<0)
{
return;
}
if(i>=n || j>=m)
{
return;
}
if(grid[i][j]==0 || visited[i][j]==1)
{
return;
}
visited[i][j]=1;
dfs(visited,i+1,j);
dfs(visited,i-1,j);
dfs(visited,i,j+1);
dfs(visited,i,j-1);
}
int no_of_islands()
{
int cnt=0;
// n=grid.size();
// m=grid[0].size();
vector<vector<int>>visited;
for(int i=0;i<n;i++)
{
vector<int>temp;
for(int j=0;j<m;j++)
{
temp.push_back(-1);
}
visited.push_back(temp);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(visited[i][j]==-1 && grid[i][j]==1)
{
dfs(visited,i,j);
cnt++;
}
}
}
return cnt;
}
vector<int> numOfIslands(int n1, int m1, vector<vector<int>> &operators) {
// code here
n=n1;
m=m1;
for(int i=0;i<n;i++)
{
vector<int>t;
for(int j=0;j<m;j++)
{
t.push_back(0);
}
grid.push_back(t);
}
vector<int>ans;
for(int i=0;i<operators.size();i++)
{
grid[operators[i][0]][operators[i][1]]=1;
int n=no_of_islands();
ans.push_back(n);
}
return ans;
}
};