当前位置:网站首页>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
}
边栏推荐
- Circuit breaker: use of hystrix
- Leetcode question brushing (5.28) hash table
- Flash return file download
- "Friendship and righteousness" of the center for national economy and information technology: China's friendship wine - the "unparalleled loyalty and righteousness" of the solidarity group released th
- Permutation and combination function
- wincc7.5下载安装教程(Win10系统)
- LDAP application (4) Jenkins access
- Secure captcha (unsafe verification code) of DVWA range
- National economic information center "APEC industry +": economic data released at the night of the Spring Festival | observation of stable strategy industry fund
- 好用的TCP-UDP_debug工具下载和使用
猜你喜欢

The State Economic Information Center "APEC industry +" Western Silicon Valley will invest 2trillion yuan in Chengdu Chongqing economic circle, which will surpass the observation of Shanghai | stable

化不掉的钟薛高,逃不出网红产品的生命周期

Summary of phased use of sonic one-stop open source distributed cluster cloud real machine test platform

leetcode刷题 (5.28) 哈希表

Wincc7.5 download and installation tutorial (win10 system)

CISP-PTE实操练习讲解

【刷题】牛客网面试必刷TOP101

NFT smart contract release, blind box, public offering technology practice -- jigsaw puzzle

2022 Inner Mongolia latest water conservancy and hydropower construction safety officer simulation examination questions and answers
![[MySQL] database stored procedure and storage function clearance tutorial (full version)](/img/27/e775e03b77c7195216bc50c5cbefb4.png)
[MySQL] database stored procedure and storage function clearance tutorial (full version)
随机推荐
你想知道的ArrayList知识都在这
hcip--mpls
LDAP application (4) Jenkins access
Fibonacci sequence
Introduction to backup and recovery Cr
使用 BR 备份 TiDB 集群数据到兼容 S3 的存储
The Vice Minister of the Ministry of industry and information technology of "APEC industry +" of the national economic and information technology center led a team to Sichuan to investigate the operat
Circuit breaker: use of hystrix
Step by step guide to setting NFT as an ens profile Avatar
Convolution, pooling, activation function, initialization, normalization, regularization, learning rate - Summary of deep learning foundation
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
LDAP应用篇(4)Jenkins接入
Easy to use tcp-udp_ Debug tool download and use
Summary of MySQL index failure scenarios
wincc7.5下载安装教程(Win10系统)
3. File operation 3-with
A Closer Look at How Fine-tuning Changes BERT
Restore backup data on S3 compatible storage with tidb lightning
Chinese Remainder Theorem (Sun Tzu theorem) principle and template code
synchronized 解决共享带来的问题