当前位置:网站首页>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
}
边栏推荐
- Vocabulary notes for postgraduate entrance examination (3)
- Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
- LDAP應用篇(4)Jenkins接入
- Golang DNS 随便写写
- Day29-t77 & t1726-2022-02-13-don't answer by yourself
- 远程存储访问授权
- wincc7.5下载安装教程(Win10系统)
- 你想知道的ArrayList知识都在这
- 【MySQL】日志
- On the day of resignation, jd.com deleted the database and ran away, and the programmer was sentenced
猜你喜欢
IOT -- interpreting the four tier architecture of the Internet of things
[research materials] 2022 China yuancosmos white paper - Download attached
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
From monomer structure to microservice architecture, introduction to microservices
IoT -- 解读物联网四层架构
Leetcode question brushing record | 203_ Remove linked list elements
Asia Pacific Financial Media | art cube of "designer universe": Guangzhou community designers achieve "great improvement" in urban quality | observation of stable strategy industry fund
"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
Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
Make learning pointer easier (3)
随机推荐
MFC 给列表控件发送左键单击、双击、以及右键单击消息
好用的TCP-UDP_debug工具下载和使用
Wireshark grabs packets to understand its word TCP segment
Colorlog结合logging打印有颜色的日志
灰度升级 TiDB Operator
Pointer advanced --- pointer array, array pointer
Online yaml to CSV tool
在 uniapp 中使用阿里图标
Understanding of law of large numbers and central limit theorem
化不掉的钟薛高,逃不出网红产品的生命周期
Easy to use tcp-udp_ Debug tool download and use
[luatos-air551g] 6.2 repair: restart caused by line drawing
Résumé des diagrammes de description des broches de la série ESP
1. Color inversion, logarithmic transformation, gamma transformation source code - miniopencv from zero
Go learning notes (3) basic types and statements (2)
Analysis of pointer and array written test questions
Migrate data from SQL files to tidb
【MySQL】日志
Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
Golang DNS 随便写写