当前位置:网站首页>Golang force buckle leetcode 1020 Number of enclaves
Golang force buckle leetcode 1020 Number of enclaves
2022-07-06 08:22:00 【cheems~】
1020. The number of enclaves
1020. The number of enclaves
Answer key
BFS perhaps DFS, The idea of this question is to take the border as the search point , Then what can be searched is what can be accessed , In the code vis, You can calculate what you can't find by traversing , That's the answer
Code
package main
func numEnclaves(grid [][]int) int {
var dirs = []struct{
x, y int }{
{
-1, 0}, {
1, 0}, {
0, -1}, {
0, 1}}
var vis = make([][]bool, len(grid))
for i, v := range grid {
vis[i] = make([]bool, len(v))
}
var dfs func(int, int)
dfs = func(x, y int) {
if x < 0 || x >= len(grid) || y < 0 || y >= len(grid[0]) || vis[x][y] || grid[x][y] == 0 {
return
}
vis[x][y] = true
for _, v := range dirs {
dfs(x+v.x, y+v.y)
}
}
for i := range grid {
dfs(i, 0)
dfs(i, len(grid[0])-1)
}
for i := 0; i <= len(grid[0])-1; i++ {
dfs(0, i)
dfs(len(grid)-1, i)
}
ans := 0
for i, v := range grid {
for j := range v {
if grid[i][j] == 1 && vis[i][j] == false {
ans++
}
}
}
return ans
}
边栏推荐
- 649. Dota2 Senate
- 根据csv文件某一列字符串中某个数字排序
- MFC sends left click, double click, and right click messages to list controls
- How to use information mechanism to realize process mutual exclusion, process synchronization and precursor relationship
- 2022 Inner Mongolia latest water conservancy and hydropower construction safety officer simulation examination questions and answers
- [t31zl intelligent video application processor data]
- Online yaml to CSV tool
- Remote storage access authorization
- 使用 TiDB Lightning 恢复 S3 兼容存储上的备份数据
- 1. Color inversion, logarithmic transformation, gamma transformation source code - miniopencv from zero
猜你喜欢
Yyds dry goods inventory three JS source code interpretation eventdispatcher
Easy to use tcp-udp_ Debug tool download and use
Configuring OSPF load sharing for Huawei devices
Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
Learn Arduino with examples
From monomer structure to microservice architecture, introduction to microservices
Fibonacci sequence
你想知道的ArrayList知识都在这
Convolution, pooling, activation function, initialization, normalization, regularization, learning rate - Summary of deep learning foundation
Circular reference of ES6 module
随机推荐
Go learning notes (3) basic types and statements (2)
Introduction to backup and recovery Cr
将 NFT 设置为 ENS 个人资料头像的分步指南
2022 Inner Mongolia latest water conservancy and hydropower construction safety officer simulation examination questions and answers
根据csv文件某一列字符串中某个数字排序
JS select all and tab bar switching, simple comments
灰度升级 TiDB Operator
The resources of underground pipe holes are tight, and the air blowing micro cable is not fragrant?
图像融合--挑战、机遇与对策
Secure captcha (unsafe verification code) of DVWA range
vulnhub hackme: 1
备份与恢复 CR 介绍
LDAP应用篇(4)Jenkins接入
[Yugong series] February 2022 U3D full stack class 010 prefabricated parts
ESP系列引脚说明图汇总
从 SQL 文件迁移数据到 TiDB
Nft智能合约发行,盲盒,公开发售技术实战--拼图篇
ESP series pin description diagram summary
Summary of MySQL index failure scenarios
String to leading 0