当前位置:网站首页>[leetcode daily clock in] 1020 Number of enclaves
[leetcode daily clock in] 1020 Number of enclaves
2022-07-06 22:11:00 【yyhnet. cn】
class Solution:
def numEnclaves(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
vis = [[False] * n for _ in range(m)]
def dfs(r: int, c: int) -> None:
if r < 0 or r >= m or c < 0 or c >= n or grid[r][c] == 0 or vis[r][c]:
return
vis[r][c] = True
for x, y in ((r - 1, c), (r + 1, c), (r, c - 1), (r, c + 1)):
dfs(x, y)
for i in range(m):
dfs(i, 0)
dfs(i, n - 1)
for j in range(1, n - 1):
dfs(0, j)
dfs(m - 1, j)
return sum(grid[i][j] and not vis[i][j] for i in range(1, m - 1) for j in range(1, n - 1))
1. adopt DFS
Traversal can calculate whether the current point can go to the ground , If the current point is not outside the boundary and is land , And it is marked as True
, And continue to traverse the surrounding points .
2. By judging the number of ground that cannot be walked out , That is, the current statistics is False
Land parcel of , You can get the answer .
边栏推荐
- Uni app app half screen continuous code scanning
- 插入排序与希尔排序
- [Yu Yue education] reference materials for surgical skills teaching in Tongji University
- Realization of epoll reactor model
- 保存和检索字符串
- hdu 4912 Paths on the tree(lca+馋)
- GPS从入门到放弃(十二)、 多普勒定速
- Broadcast variables and accumulators in spark
- 20 large visual screens that are highly praised by the boss, with source code templates!
- AI 企业多云存储架构实践 | 深势科技分享
猜你喜欢
Reset Mikrotik Routeros using netinstall
GPS从入门到放弃(十四)、电离层延时
ZABBIX proxy server and ZABBIX SNMP monitoring
GPS from getting started to giving up (12), Doppler constant speed
bat脚本学习(一)
Adjustable DC power supply based on LM317
Aggregate function with key in spark
【MySQL】Online DDL详解
Vit paper details
GPS从入门到放弃(十七) 、对流层延时
随机推荐
GPS从入门到放弃(十三)、接收机自主完好性监测(RAIM)
Unity3D学习笔记6——GPU实例化(1)
GPS从入门到放弃(十四)、电离层延时
Oracle Performance Analysis 3: introduction to tkprof
MPLS experiment
GPS从入门到放弃(十五)、DCB差分码偏差
About the professional ethics of programmers, let's talk about it from the way of craftsmanship and neatness
GPS from getting started to giving up (XI), differential GPS
GPS从入门到放弃(十六)、卫星时钟误差和卫星星历误差
保存和检索字符串
hdu 4912 Paths on the tree(lca+馋)
2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks
Bat script learning (I)
MySQL related terms
Management background --4, delete classification
Force buckle 575 Divide candy
Four data streams of grpc
墨西哥一架飞往美国的客机起飞后遭雷击 随后安全返航
GPS from getting started to giving up (16), satellite clock error and satellite ephemeris error
GPS from getting started to giving up (XVIII), multipath effect