-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount_Sub_Islands.cpp
33 lines (33 loc) · 1001 Bytes
/
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
class Solution {
public:
void dfs(vector<vector<int>>&grid2,vector<vector<int>>&grid1,bool &flag,int i,int j){
if(i < 0 || i >= grid2.size() || j < 0 || j >= grid2[0].size() || grid2[i][j] == 0){
return;
}
if(grid1[i][j] == 0){
flag = false;
}
grid2[i][j] = 0;
dfs(grid2,grid1,flag,i+1,j);
dfs(grid2,grid1,flag,i-1,j);
dfs(grid2,grid1,flag,i,j+1);
dfs(grid2,grid1,flag,i,j-1);
}
int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {
int ans = 0;
int m = grid2.size();
int n = grid2[0].size();
for(int i = 0;i < m; i++){
for(int j = 0; j < n; j++){
if(grid2[i][j] == 1){
bool flag = true;
dfs(grid2,grid1,flag,i,j);
if(flag){
ans++;
}
}
}
}
return ans;
}
};