给你两个 m x n
的二进制矩阵 grid1
和 grid2
,它们只包含 0
(表示水域)和 1
(表示陆地)。一个 岛屿 是由 四个方向 (水平或者竖直)上相邻的 1
组成的区域。任何矩阵以外的区域都视为水域。
如果 grid2
的一个岛屿,被 grid1
的一个岛屿 完全 包含,也就是说 grid2
中该岛屿的每一个格子都被 grid1
中同一个岛屿完全包含,那么我们称 grid2
中的这个岛屿为 子岛屿 。
请你返回 grid2
中 子岛屿 的 数目 。
示例 1:
输入:grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]] 输出:3 解释:如上图所示,左边为 grid1 ,右边为 grid2 。 grid2 中标红的 1 区域是子岛屿,总共有 3 个子岛屿。
示例 2:
输入:grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]] 输出:2 解释:如上图所示,左边为 grid1 ,右边为 grid2 。 grid2 中标红的 1 区域是子岛屿,总共有 2 个子岛屿。
提示:
m == grid1.length == grid2.length
n == grid1[i].length == grid2[i].length
1 <= m, n <= 500
grid1[i][j]
和grid2[i][j]
都要么是0
要么是1
。
C++
class Solution {
public:
void dfs(vector<vector<int>>& grid1, vector<vector<int>>& grid2, vector<vector<int>>& flag, int r, int c, int& isOverriding) {
if(grid1[r][c]==0) {
isOverriding=0;
}
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
for(int k=0;k<4;k++) {
int y=r+dy[k];
int x=c+dx[k];
if(y>=0 && y<grid1.size() && x>=0 && x<grid1[0].size() && grid2[y][x]==1 && flag[y][x]==0) {
flag[y][x]=1;
dfs(grid1,grid2,flag,y,x,isOverriding);
}
}
}
int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {
int m=grid1.size();
int n=grid1[0].size();
int ans=0;
vector<vector<int>> flag(m,vector<int>(n,0));
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
if(0==flag[i][j] && grid2[i][j]==1) {
flag[i][j]=1;
int isOverriding = 1;
dfs(grid1,grid2,flag,i,j,isOverriding);
ans+=isOverriding;
}
}
}
return ans;
}
};
java
class Solution {
private int isOvr = 1;
public void dfs(int[][] grid1, int[][] grid2, int[][] flag, int r, int c) {
if (0 == grid1[r][c]) {
isOvr = 0;
}
int dy[] = {1, -1, 0, 0};
int dx[] = {0, 0, 1, -1};
for (int k = 0; k < 4; k++) {
int y = r + dy[k];
int x = c + dx[k];
if (y >= 0 && y < grid1.length && x >= 0 && x < grid1[0].length && 0 == flag[y][x] && 1 == grid2[y][x]) {
flag[y][x] = 1;
dfs(grid1, grid2, flag, y, x);
}
}
}
public int countSubIslands(int[][] grid1, int[][] grid2) {
int m = grid1.length;
int n = grid1[0].length;
int flag[][] = new int[m][n];
int ans = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (0 == flag[i][j] && 1 == grid2[i][j]) {
flag[i][j] = 1;
dfs(grid1, grid2, flag, i, j);
ans += isOvr;
isOvr = 1;
}
}
}
return ans;
}
}
python
class Solution:
isOverriding = 1
def dfs(self, grid1, grid2, flag, r, c):
if 0 == grid1[r][c]:
self.isOverriding = 0
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
for k in range(4):
y = r + dy[k]
x = c + dx[k]
if y >= 0 and y < len(grid1) and x >= 0 and x < len(grid1[0]) and 0 == flag[y][x] and 1 == grid2[y][x]:
flag[y][x] = 1
self.dfs(grid1, grid2, flag, y, x)
def countSubIslands(self, grid1: List[List[int]], grid2: List[List[int]]) -> int:
m = len(grid1)
n = len(grid1[0])
ans = 0
flag = [[0 for j in range(n)] for i in range(m)]
for i in range(m):
for j in range(n):
if 1 == grid2[i][j] and 0 == flag[i][j]:
flag[i][j] = 1
self.dfs(grid1, grid2, flag, i, j)
ans += self.isOverriding
self.isOverriding = 1
return ans