当前位置:网站首页>One question per day 1020 Number of enclaves
One question per day 1020 Number of enclaves
2022-07-05 06:12:00 【A big pigeon】
topic :
Give you a size of m x n The binary matrix of grid , among 0 Represents an ocean cell 、1 Represents a land cell .
once Move It refers to walking from one land cell to another ( On 、 Next 、 Left 、 Right ) Land cells or across grid The boundary of the .
Return to grid unable Land that leaves the grid boundary in any number of movements ( enclave ) The number of cells .
Explain : use 2 Mark the land you can leave .
First mark the outermost land 2. And then from 2 set out , Depth first search the surrounding land is marked 2.
Last grid The unmarked land in is the enclave .
class Solution:
def numEnclaves(self, grid: List[List[int]]) -> int:
def dfs(i,j):
nonlocal grid,m,n
if i<0 or j<0 or i>=m or j >=n :
return
if grid[i][j]==1:
grid[i][j] = 2 # Departable land
dfs(i+1,j)
dfs(i-1,j)
dfs(i,j-1)
dfs(i,j+1)
return
m, n = len(grid), len(grid[0])
cnt = 0
for i in range(m):
grid[i][0] = 2 if grid[i][0] == 1 else 0
grid[i][n-1] = 2 if grid[i][n-1] == 1 else 0
for j in range(n):
grid[0][j] = 2 if grid[0][j] == 1 else 0
grid[m-1][j] = 2 if grid[m-1][j] == 1 else 0
for i in range(m):
for j in range(n):
if grid[i][j] == 2:
dfs(i+1,j)
dfs(i-1,j)
dfs(i,j-1)
dfs(i,j+1)
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
cnt += 1
return cnt
边栏推荐
猜你喜欢

1.13 - RISC/CISC
![[jailhouse article] performance measurements for hypervisors on embedded ARM processors](/img/c0/4843f887f77b80e3b2329e12d28987.png)
[jailhouse article] performance measurements for hypervisors on embedded ARM processors

Traditional databases are gradually "difficult to adapt", and cloud native databases stand out

R语言【数据集的导入导出】
![[jailhouse article] look mum, no VM exits](/img/fe/87e0851d243f14dff96ef1bc350e50.png)
[jailhouse article] look mum, no VM exits

Appium automation test foundation - Summary of appium test environment construction

Smart construction site "hydropower energy consumption online monitoring system"

4. 对象映射 - Mapping.Mapster

The connection and solution between the shortest Hamilton path and the traveling salesman problem

leetcode-6111:螺旋矩阵 IV
随机推荐
Typical use cases for knapsacks, queues, and stacks
Dichotomy, discretization, etc
leetcode-31:下一个排列
1996. number of weak characters in the game
LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树
Real time clock (RTC)
[cloud native] record of feign custom configuration of microservices
可变电阻器概述——结构、工作和不同应用
In depth analysis of for (VaR I = 0; I < 5; i++) {settimeout (() => console.log (I), 1000)}
[rust notes] 15 string and text (Part 1)
Simple knapsack, queue and stack with deque
Daily question 2006 Number of pairs whose absolute value of difference is k
1039 Course List for Student
Navicat连接Oracle数据库报错ORA-28547或ORA-03135
redis发布订阅命令行实现
1.15 - 输入输出系统
【Rust 笔记】14-集合(上)
Dynamic planning solution ideas and summary (30000 words)
Leetcode-6109: number of people who know secrets
leetcode-556:下一个更大元素 III