1905. 统计子岛屿
java:DFS
class Solution {Boolean flag = true;private void dfs(int x, int y, int m, int n, int[][] grid1, int[][] grid2) {if (x < 0 || x >= m || y < 0 || y >= n || grid2[x][y] != 1) { // 包含关系:遍历最小范围,如果其中一个点不被大范围的包含,就不是包含关系return;}if (grid1[x][y] != 1) {flag = false;}grid2[x][y] = 0;dfs(x - 1, y, m, n, grid1, grid2);dfs(x + 1, y, m, n, grid1, grid2);dfs(x, y - 1, m, n, grid1, grid2);dfs(x, y + 1, m, n, grid1, grid2);}public int countSubIslands(int[][] grid1, int[][] grid2) {int m = grid2.length;int n = grid2[0].length;int cnt = 0;for (int i = 0; i < m; ++i) {for(int j = 0; j < n; ++j) {if (grid2[i][j] == 1) {flag = true;dfs(i, j, m, n, grid1, grid2);if(flag) {cnt++;}}}}return cnt;}
}