当前位置:网站首页>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
边栏推荐
- Leetcode-9: palindromes
- Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module
- Sword finger offer II 058: schedule
- 【Rust 笔记】16-输入与输出(上)
- js快速将json数据转换为url参数
- LeetCode 1200. Minimum absolute difference
- Appium foundation - use the first demo of appium
- 1040 Longest Symmetric String
- Simply sort out the types of sockets
- 中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
猜你喜欢

wordpress切换页面,域名变回了IP地址

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

Leetcode-6108: decrypt messages

QQ电脑版取消转义符输入表情

快速使用Amazon MemoryDB并构建你专属的Redis内存数据库

中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章

MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!

LeetCode 0107.二叉树的层序遍历II - 另一种方法

Brief introduction to tcp/ip protocol stack

Appium自动化测试基础 — Appium测试环境搭建总结
随机推荐
R language [import and export of dataset]
Wazuh开源主机安全解决方案的简介与使用体验
leetcode-9:回文数
Introduction et expérience de wazuh open source host Security Solution
wordpress切换页面,域名变回了IP地址
LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively
Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module
1.14 - assembly line
1.13 - RISC/CISC
R语言【数据集的导入导出】
Introduction and experience of wazuh open source host security solution
Daily question 1984 Minimum difference in student scores
Bit mask of bit operation
[practical skills] how to do a good job in technical training?
Leetcode-31: next spread
[jailhouse article] performance measurements for hypervisors on embedded ARM processors
1041 Be Unique
liunx启动redis
“磐云杯”中职网络安全技能大赛A模块新题
927. 三等分 模拟