Skip to content

Latest commit

 

History

History
221 lines (185 loc) · 6.88 KB

File metadata and controls

221 lines (185 loc) · 6.88 KB

中文文档

Description

You are given two m x n binary matrices grid1 and grid2 containing only 0's (representing water) and 1's (representing land). An island is a group of 1's connected 4-directionally (horizontal or vertical). Any cells outside of the grid are considered water cells.

An island in grid2 is considered a sub-island if there is an island in grid1 that contains all the cells that make up this island in grid2.

Return the number of islands in grid2 that are considered sub-islands.

 

Example 1:

Input: 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]]
Output: 3
Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.

Example 2:

Input: 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]]
Output: 2 
Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.

 

Constraints:

  • m == grid1.length == grid2.length
  • n == grid1[i].length == grid2[i].length
  • 1 <= m, n <= 500
  • grid1[i][j] and grid2[i][j] are either 0 or 1.

Solutions

DFS.

Python3

class Solution:
    def countSubIslands(self, grid1: List[List[int]], grid2: List[List[int]]) -> int:
        def dfs(grid1, grid2, i, j, m, n) -> bool:
            res = grid1[i][j] == 1
            grid2[i][j] = 0
            for x, y in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
                a, b = i + x, j + y
                if a >= 0 and a < m and b >= 0 and b < n and grid2[a][b] == 1:
                    if not dfs(grid1, grid2, a, b, m, n):
                        res = False
            return res
        
        m, n = len(grid1), len(grid1[0])
        count = 0
        for i in range(m):
            for j in range(n):
                if grid2[i][j] == 1 and dfs(grid1, grid2, i, j, m, n):
                    count += 1
        return count

Java

class Solution {
    private int[][] directions = {{0, 1}, {0, - 1}, {1, 0}, {-1, 0}};

    public int countSubIslands(int[][] grid1, int[][] grid2) {
        int m = grid1.length, n = grid1[0].length;
        int count = 0;
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (grid2[i][j] == 1 && dfs(grid1, grid2, i, j, m, n)) {
                    ++count;
                }
            }
        }
        return count;
    }

    private boolean dfs(int[][] grid1, int[][] grid2, int i, int j, int m, int n) {
        boolean res = grid1[i][j] == 1;
        grid2[i][j] = 0;

        for (int[] direction : directions) {
            int a = i + direction[0], b = j + direction[1];
            if (a >= 0 && a < m && b >= 0 && b < n && grid2[a][b] == 1) {
                if (!dfs(grid1, grid2, a, b, m, n)) {
                    res = false;
                }
            }
        }
        return res;
    }
}

TypeScript

function countSubIslands(grid1: number[][], grid2: number[][]): number {
    let m = grid1.length, n = grid1[0].length;
    let ans = 0;
    for (let i = 0; i < m; ++i) {
        for (let j = 0; j < n; ++j) {
            if (grid2[i][j] == 1 && dfs(grid1, grid2, i, j)) {
                ++ans;
            }
        }
    }
    return ans;
};

function dfs(grid1: number[][], grid2: number[][], i: number, j: number): boolean {
    let m = grid1.length, n = grid1[0].length;
    let ans = true;
    if (grid1[i][j] == 0) {
        ans = false;
    }
    grid2[i][j] = 0;
    for (let [dx, dy] of [[0, 1], [0, -1], [1, 0], [-1, 0]]) {
        let x = i + dx, y = j + dy;
        if (x < 0 || x > m - 1 || y < 0 || y > n - 1 || grid2[x][y] == 0) {
            continue;
        }
        if (!dfs(grid1, grid2, x, y)) {
            ans = false;
        }
    }
    return ans;
}

C++

class Solution {
public:
    int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {
        int m = grid1.size(), n = grid1[0].size();
        int count = 0;
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (grid2[i][j] == 1 && dfs(grid1, grid2, i, j, m, n)) {
                    ++count;
                }
            }
        }
        return count;
    }

private:
    vector<vector<int>> directions = {{0, 1}, {0, - 1}, {1, 0}, {-1, 0}};
    bool dfs(vector<vector<int>>& grid1, vector<vector<int>>& grid2, int i, int j, int m, int n) {
        bool res = grid1[i][j] == 1;
        grid2[i][j] = 0;

        for (auto direction : directions) {
            int a = i + direction[0], b = j + direction[1];
            if (a >= 0 && a < m && b >= 0 && b < n && grid2[a][b] == 1) {
                if (!dfs(grid1, grid2, a, b, m, n)) {
                    res = false;
                }
            }
        }
        return res;
    }
};

Go

func countSubIslands(grid1 [][]int, grid2 [][]int) int {
	m, n := len(grid1), len(grid1[0])
	count := 0
	for i := 0; i < m; i++ {
		for j := 0; j < n; j++ {
			if grid2[i][j] == 1 && dfs(grid1, grid2, i, j, m, n) {
				count++
			}
		}
	}
	return count
}

func dfs(grid1 [][]int, grid2 [][]int, i, j, m, n int) bool {
	res := grid1[i][j] == 1
	grid2[i][j] = 0
	directions := [4][2]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}
	for _, direction := range directions {
		a, b := i+direction[0], j+direction[1]
		if a >= 0 && a < m && b >= 0 && b < n && grid2[a][b] == 1 {
			if !dfs(grid1, grid2, a, b, m, n) {
				res = false
			}
		}
	}
	return res
}

...