The goal is to identify islands in grid2 that are fully contained within the corresponding islands in grid1. This involves using Depth-First Search (DFS) to explore each island in grid2 and checking whether every part of it exists in grid1.
This approach uses DFS to explore each cell in grid2. When an island (group of connected 1's) is found in grid2, the DFS checks whether every cell in that island is also a 1 in the corresponding position in grid1. If the entire island is contained within an island in grid1, it is counted as a sub-island.
-
Initialization:
- The
dirsarray defines the four possible directions (right, down, left, up) for moving in the grid. - The
nandmvariables store the dimensions of the grids.
- The
-
DFS Function:
- The
dfsfunction checks whether the current cell(i, j)ingrid2is part of a valid island. - If the current cell is out of bounds or contains water (
0), it returnstrue, meaning this part doesn't invalidate the sub-island condition. - The cell is marked as visited by setting
grid2[i][j]to0to prevent revisiting. - The result
resstarts asgrid1[i][j], meaning if the corresponding cell ingrid1is water, it’s not a valid sub-island. - The DFS then recursively explores all four possible directions. The result is updated by combining the results of these recursive calls with the current
resusing logicalANDto ensure all parts of the island ingrid2must matchgrid1.
- The
-
Counting Sub-Islands:
- Iterate through all cells in
grid2. If a cell is part of an island (grid2[i][j] == 1), initiate a DFS from that cell. - If the DFS returns
true, it means this entire island ingrid2is a sub-island ofgrid1, so increment the result counter.
- Iterate through all cells in
-
Final Return:
- The function returns the count of sub-islands found.
- Time complexity: O(n × m), where
nis the number of rows andmis the number of columns in the grids. Every cell is visited once during the DFS. - Space complexity: O(n × m) in the worst case due to the recursive stack.
const int dirs[] = {0, 1, 0, -1, 0};
class Solution {
public:
int n, m;
bool dfs(vector<vector<int>>& grid1, vector<vector<int>>& grid2, int i, int j) {
if (i < 0 || i == n || j < 0 || j == m || grid2[i][j] == 0) {
return true;
}
grid2[i][j] = 0;
bool res = grid1[i][j];
for (int d = 0; d < 4; d++) {
res = dfs(grid1, grid2, i + dirs[d], j + dirs[d + 1]) && res;
}
return res;
}
int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {
n = grid1.size();
m = grid1[0].size();
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid2[i][j] == 1 && dfs(grid1, grid2, i, j) == true) {
++res;
}
}
}
return res;
}
};