-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber_of_islands.cpp
More file actions
82 lines (78 loc) · 2.57 KB
/
number_of_islands.cpp
File metadata and controls
82 lines (78 loc) · 2.57 KB
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
76
77
78
79
80
81
82
https://practice.geeksforgeeks.org/problems/find-the-number-of-islands/1?utm_source=gfg&utm_medium=article&utm_campaign=bottom_sticky_on_article
// this link is for 8 directions
// for 4 directions do
/*
int dx[ ] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
*/
//Find the number of islands
//approach -1 dfs
void dfs(int row,int col,vector<vector<char>>&grid,vector<vector<int>>&vis,int n,int m){
vis[row][col] =1;
int dx[8] = {-1,-1,0,1,1,1,0,-1};
int dy[8] = {0,1,1,1,0,-1,-1,-1};
for(int i= 0;i<8;i++){
int nrow = row+dx[i];
int ncol = col+ dy[i];
if(nrow>=0&& nrow<n && ncol>= 0 && ncol<m &&
!vis[nrow][ncol] && grid[nrow][ncol] == '1'){
dfs(nrow,ncol,grid,vis,n,m);
}
}
}
void bfsnumIslands(int row,int col,vector<vector<char>>&grid,vector<vector<int>>&vis,int n,int m){
vis[row][col]=1;
queue<pair<int,int>>q;
q.push({row,col});
int dx[8] = {-1,-1,0,1,1,1,0,-1};
int dy[8] = {0,1,1,1,0,-1,-1,-1};
/*
Instead of using dx and dy we could've used
for(int delRow = -1;delRow<=1;delRow++){
for(int delCol =-1;delCol<=1;delCol++){
and same find nrow using delRow + r
and delCol + c
*/
while(!q.empty()){
int r=q.front().first;
int c=q.front().second;
q.pop();
for(int i= 0;i<8;i++)
{
int nrow = dx[i]+r;
int ncol = dy[i]+c;
if(nrow>=0&& nrow<n && ncol>=0 && ncol<m && !vis[nrow][ncol] && grid[nrow][ncol] == '1'){
vis[nrow][ncol] =1;
q.push({nrow,ncol});
}
}
}
}
int numIslands(vector<vector<char>>& grid) {
//dfs
int n = grid.size(); int m = grid[0].size();
vector<vector<int>>vis(n,vector<int>(m,0));
int cnt=0;
for(int i =0;i<n;i++)
{
for(int j = 0;j<m;j++){
if(!vis[i][j] && grid[i][j] == '1'){
cnt++;
dfs(i,j,grid,vis,n,m);
}
}
}
return cnt;
// bfs
int n = grid.size();int m = grid[0].size(); int cnt=0;
vector<vector<int>>vis(n,vector<int>(m,0));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(!vis[i][j] && grid[i][j]== '1'){
cnt++;
bfsnumIslands(i,j,grid,vis,n,m);
}
}
}
return cnt;
}