当前位置:网站首页>[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 .
边栏推荐
- Maximum product of three numbers in question 628 of Li Kou
- Adjustable DC power supply based on LM317
- Unity3D学习笔记6——GPU实例化(1)
- What a new company needs to practice and pay attention to
- Powerful domestic API management tool
- 设置状态栏样式Demo
- 3DMax指定面贴图
- HDU 2008 数字统计
- [Yu Yue education] reference materials for surgical skills teaching in Tongji University
- Oracle control file and log file management
猜你喜欢
小满网络模型&http1-http2 &浏览器缓存
GNN, please deepen your network layer~
关于程序员的职业操守,从《匠艺整洁之道》谈起
Why is the cluster mode of spark on Yan better than the client mode
About the professional ethics of programmers, let's talk about it from the way of craftsmanship and neatness
UNI-Admin基础框架怎么关闭创建超级管理员入口?
LeetCode:1189. The maximum number of "balloons" -- simple
Persistence / caching of RDD in spark
Why rdd/dataset is needed in spark
AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
随机推荐
插入排序与希尔排序
Codeforces Round #274 (Div. 2) –A Expression
Support multiple API versions in flask
Oracle Performance Analysis 3: introduction to tkprof
CCNA-思科网络 EIGRP协议
Qt | UDP广播通信、简单使用案例
设置状态栏样式Demo
Yyds dry goods inventory C language recursive implementation of Hanoi Tower
What a new company needs to practice and pay attention to
Broadcast variables and accumulators in spark
【MySQL】Online DDL详解
GPS从入门到放弃(十二)、 多普勒定速
LeetCode:1189. The maximum number of "balloons" -- simple
GPS du début à l'abandon (XIII), surveillance autonome de l'intégrité du récepteur (raim)
bat脚本学习(一)
Classic sql50 questions
二叉(搜索)树的最近公共祖先 ●●
Depth first traversal (DFS) and breadth first traversal (BFS)
GPS from getting started to giving up (12), Doppler constant speed
华为在多个行业同时出击,吓人的技术让欧美企业瑟瑟发抖