当前位置:网站首页>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
}
边栏推荐
- 1202 character lookup
- 将 NFT 设置为 ENS 个人资料头像的分步指南
- Make learning pointer easier (3)
- 从 SQL 文件迁移数据到 TiDB
- Easy to use tcp-udp_ Debug tool download and use
- Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
- 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
- Remote storage access authorization
- 使用 Dumpling 备份 TiDB 集群数据到兼容 S3 的存储
- 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
猜你喜欢
"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
IP lab, the first weekly recheck
2022 Inner Mongolia latest water conservancy and hydropower construction safety officer simulation examination questions and answers
wincc7.5下载安装教程(Win10系统)
ESP系列引脚說明圖匯總
Hcip day 16
Learn Arduino with examples
Leetcode question brushing record | 203_ Remove linked list elements
[research materials] 2021 China online high growth white paper - Download attached
[secretly kill little partner pytorch20 days -day01- example of structured data modeling process]
随机推荐
使用 Dumpling 备份 TiDB 集群数据到兼容 S3 的存储
LDAP应用篇(4)Jenkins接入
IOT -- interpreting the four tier architecture of the Internet of things
使用 BR 备份 TiDB 集群数据到兼容 S3 的存储
化不掉的钟薛高,逃不出网红产品的生命周期
Golang DNS 随便写写
Analysis of pointer and array written test questions
从 CSV 文件迁移数据到 TiDB
Leetcode question brushing (5.31) string
Migrate data from CSV files to tidb
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
好用的TCP-UDP_debug工具下载和使用
Let the bullets fly for a while
Sanzi chess (C language)
Go learning notes (3) basic types and statements (2)
Wincc7.5 download and installation tutorial (win10 system)
ESP series pin description diagram summary
Chinese Remainder Theorem (Sun Tzu theorem) principle and template code
Understanding of law of large numbers and central limit theorem
【MySQL】日志