You are given an m x n
binary matrix grid
, where 0
represents a sea cell and 1
represents a land cell.
A move consists of walking from one land cell to another adjacent (4-directionally) land cell or walking off the boundary of the grid
.
Return the number of land cells in grid
for which we cannot walk off the boundary of the grid in any number of moves.
Example 1:
Input: grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]] Output: 3 Explanation: There are three 1s that are enclosed by 0s, and one 1 that is not enclosed because its on the boundary.
Example 2:
Input: grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]] Output: 0 Explanation: All 1s are either on the boundary or can reach the boundary.
Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 500
grid[i][j]
is either0
or1
.
DFS or Union find.
DFS:
class Solution:
def numEnclaves(self, grid: List[List[int]]) -> int:
def dfs(i, j):
grid[i][j] = 0
for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
x, y = i + a, j + b
if 0 <= x < m and 0 <= y < n and grid[x][y] == 1:
dfs(x, y)
m, n = len(grid), len(grid[0])
for i in range(m):
for j in range(n):
if grid[i][j] == 1 and (i == 0 or i == m - 1 or j == 0 or j == n - 1):
dfs(i, j)
return sum(grid[i][j] for i in range(m) for j in range(n))
Union find:
class Solution:
def numEnclaves(self, grid: List[List[int]]) -> int:
def find(x):
if p[x] != x:
p[x] = find(p[x])
return p[x]
m, n = len(grid), len(grid[0])
p = list(range(m * n + 1))
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
if i == 0 or i == m - 1 or j == 0 or j == n - 1:
p[find(i * n + j)] = find(m * n)
else:
for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
x, y = i + a, j + b
if grid[x][y] == 1:
p[find(i * n + j)] = find(x * n + y)
return sum(grid[i][j] == 1 and find(i * n + j) != find(m * n) for i in range(m) for j in range(n))
DFS:
class Solution {
private int[][] grid;
private int m;
private int n;
public int numEnclaves(int[][] grid) {
m = grid.length;
n = grid[0].length;
this.grid = grid;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1 && (i == 0 || i == m - 1 || j == 0 || j == n - 1)) {
dfs(i, j);
}
}
}
int ans = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1) {
++ans;
}
}
}
return ans;
}
private void dfs(int i, int j) {
grid[i][j] = 0;
int[] dirs = {-1, 0, 1, 0, -1};
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k];
int y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) {
dfs(x, y);
}
}
}
}
Union find:
class Solution {
private int[] p;
public int numEnclaves(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
p = new int[m * n + 1];
for (int i = 0; i < p.length; ++i) {
p[i] = i;
}
int[] dirs = {-1, 0, 1, 0, -1};
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1) {
if (i == 0 || i == m - 1 || j == 0 || j == n - 1) {
p[find(i * n + j)] = find(m * n);
} else {
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k];
int y = j + dirs[k + 1];
if (grid[x][y] == 1) {
p[find(i * n + j)] = find(x * n + y);
}
}
}
}
}
}
int ans = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1 && find(i * n + j) != find(m * n)) {
++ans;
}
}
}
return ans;
}
private int find(int x) {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
}
}
function numEnclaves(grid: number[][]): number {
const m = grid.length,
n = grid[0].length;
let ans = 0;
let queue = [];
// 统计全部1, 临边的1加入队列
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
let cur = grid[i][j];
if (cur) {
ans++;
if (i == 0 || i == m - 1 || j == 0 || j == n - 1) {
queue.push([i, j]);
ans--;
}
}
}
}
let directions = [
[-1, 0],
[1, 0],
[0, -1],
[0, 1],
];
while (queue.length) {
let nextQueue = [];
for (let [x, y] of queue) {
for (let [dx, dy] of directions) {
let [i, j] = [x + dx, y + dy];
if (i > 0 && i < m - 1 && j > 0 && j < n - 1 && grid[i][j]) {
nextQueue.push([i, j]);
ans--;
grid[i][j] = 0;
}
}
queue = nextQueue;
}
}
return ans;
}
DFS:
class Solution {
public:
int m;
int n;
int numEnclaves(vector<vector<int>>& grid) {
m = grid.size();
n = grid[0].size();
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
if (grid[i][j] == 1 && (i == 0 || i == m - 1 || j == 0 || j == n - 1))
dfs(i, j, grid);
int ans = 0;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
if (grid[i][j] == 1)
++ans;
return ans;
}
void dfs(int i, int j, vector<vector<int>>& grid) {
grid[i][j] = 0;
vector<int> dirs = {-1, 0, 1, 0, -1};
for (int k = 0; k < 4; ++k)
{
int x = i + dirs[k];
int y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1)
dfs(x, y, grid);
}
}
};
Union find:
class Solution {
public:
vector<int> p;
int numEnclaves(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
p.resize(m * n + 1);
vector<int> dirs = {-1, 0, 1, 0, -1};
for (int i = 0; i < p.size(); ++i) p[i] = i;
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
if (grid[i][j] == 1)
{
if (i == 0 || i == m - 1 || j == 0 || j == n - 1) p[find(i * n + j)] = find(m * n);
else
{
for (int k = 0; k < 4; ++k)
{
int x = i + dirs[k];
int y = j + dirs[k + 1];
if (grid[x][y] == 1) p[find(i * n + j)] = find(x * n + y);
}
}
}
}
}
int ans = 0;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
if (grid[i][j] == 1 && find(i * n + j) != find(m * n))
++ans;
return ans;
}
int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
};
DFS:
func numEnclaves(grid [][]int) int {
m, n := len(grid), len(grid[0])
dirs := []int{-1, 0, 1, 0, -1}
var dfs func(i, j int)
dfs = func(i, j int) {
grid[i][j] = 0
for k := 0; k < 4; k++ {
x, y := i+dirs[k], j+dirs[k+1]
if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 {
dfs(x, y)
}
}
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if grid[i][j] == 1 && (i == 0 || i == m-1 || j == 0 || j == n-1) {
dfs(i, j)
}
}
}
ans := 0
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if grid[i][j] == 1 {
ans++
}
}
}
return ans
}
Union find:
func numEnclaves(grid [][]int) int {
m, n := len(grid), len(grid[0])
p := make([]int, m*n+1)
for i := range p {
p[i] = i
}
var find func(x int) int
find = func(x int) int {
if p[x] != x {
p[x] = find(p[x])
}
return p[x]
}
dirs := []int{-1, 0, 1, 0, -1}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if grid[i][j] == 1 {
if i == 0 || i == m-1 || j == 0 || j == n-1 {
p[find(i*n+j)] = find(m * n)
} else {
for k := 0; k < 4; k++ {
x, y := i+dirs[k], j+dirs[k+1]
if grid[x][y] == 1 {
p[find(i*n+j)] = find(x*n + y)
}
}
}
}
}
}
ans := 0
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if grid[i][j] == 1 && find(i*n+j) != find(m*n) {
ans++
}
}
}
return ans
}