forked from Aadi71/Data-Structures-and-Algorithms-with-Aadi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count-sub-islands.cpp
35 lines (33 loc) · 1.09 KB
/
count-sub-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
// https://leetcode.com/problems/count-sub-islands/
class Solution {
public:
void dfs(vector<vector<int>>& grid1, vector<vector<int>>& grid2, int i, int j, bool &flag){
grid2[i][j] = 2;
if(grid1[i][j] != 1) flag = false;
int a[] = {0, 0, -1, 1};
int b[] = {-1, 1, 0, 0};
for(int k = 0; k<4; k++){
int inew = i + a[k];
int jnew = j + b[k];
if(inew >= 0 && jnew >= 0 && inew < grid1.size() && jnew < grid1[0].size() && grid2[inew][jnew] == 1){
dfs(grid1, grid2, inew, jnew, flag);
}
}
}
int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {
int m = grid1.size();
int n = grid2[0].size();
bool flag = true;
int ans = 0;
for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
if(grid2[i][j] == 1){
flag = true;
dfs(grid1, grid2, i, j, flag);
if(flag == true) ans++;
}
}
}
return ans;
}
};