| 
 | 1 | +/*  | 
 | 2 | +
  | 
 | 3 | +Space: O(n²) (due to recursives calls)  | 
 | 4 | +Time: O(n²)  | 
 | 5 | +*/  | 
 | 6 | + | 
 | 7 | +bool dfs_test(int** grid1, int** grid2, int i, int j, int n, int m) {  | 
 | 8 | +    // Test if the island in grid2 is entirely in grid1 and delete the island in grid2  | 
 | 9 | +    bool ans = (grid1[i][j] == 1);  | 
 | 10 | +    grid2[i][j] = 0;  | 
 | 11 | +    if (i>0 && grid2[i-1][j]==1)  | 
 | 12 | +        ans = dfs_test(grid1, grid2, i-1, j, n ,m) && ans;  | 
 | 13 | +    if (j>0  && grid2[i][j-1]==1)  | 
 | 14 | +        ans = dfs_test(grid1, grid2, i, j-1, n ,m) && ans;  | 
 | 15 | +    if (i<(n-1)  && grid2[i+1][j]==1)  | 
 | 16 | +        ans = dfs_test(grid1, grid2, i+1, j, n ,m) && ans;  | 
 | 17 | +    if (j<(m-1)  && grid2[i][j+1]==1)  | 
 | 18 | +        ans = dfs_test(grid1, grid2, i, j+1, n ,m) && ans;  | 
 | 19 | +    return ans;  | 
 | 20 | +}  | 
 | 21 | + | 
 | 22 | +int countSubIslands(int** grid1, int grid1Size, int* grid1ColSize, int** grid2, int grid2Size, int* grid2ColSize){  | 
 | 23 | +    int cpt=0;  | 
 | 24 | +    for (int i=0; i<grid1Size; i++) {  | 
 | 25 | +        for (int j=0; j<grid1ColSize[i]; j++) {  | 
 | 26 | +            if (grid2[i][j]==1) {  | 
 | 27 | +                // Test if the island in grid2 is contained in an island in grid1  | 
 | 28 | +                if (dfs_test(grid1, grid2, i, j, grid1Size, grid1ColSize[i])){  | 
 | 29 | +                    cpt++;  | 
 | 30 | +                }  | 
 | 31 | +            }  | 
 | 32 | +        }  | 
 | 33 | +    }  | 
 | 34 | +    return cpt;  | 
 | 35 | +}  | 
0 commit comments