当前位置:网站首页>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
}
边栏推荐
- 【MySQL】数据库的存储过程与存储函数通关教程(完整版)
- 面向个性化需求的在线云数据库混合调优系统 | SIGMOD 2022入选论文解读
- 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
- Résumé des diagrammes de description des broches de la série ESP
- Restore backup data on S3 compatible storage with br
- C语言 - 位段
- 2022 Inner Mongolia latest water conservancy and hydropower construction safety officer simulation examination questions and answers
- Configuring OSPF load sharing for Huawei devices
- "Designer universe": "benefit dimension" APEC public welfare + 2022 the latest slogan and the new platform will be launched soon | Asia Pacific Financial Media
- Circuit breaker: use of hystrix
猜你喜欢
【云原生】手把手教你搭建ferry开源工单系统
Leetcode question brushing (5.28) hash table
vulnhub hackme: 1
matplotlib. Widgets are easy to use
National economic information center "APEC industry +": economic data released at the night of the Spring Festival | observation of stable strategy industry fund
[MySQL] database stored procedure and storage function clearance tutorial (full version)
Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
[untitled]
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
All the ArrayList knowledge you want to know is here
随机推荐
Use Alibaba icon in uniapp
PHP - Common magic method (nanny level teaching)
华为云OBS文件上传下载工具类
String to leading 0
Fibonacci sequence
A Closer Look at How Fine-tuning Changes BERT
MFC 给列表控件发送左键单击、双击、以及右键单击消息
"Designer universe" Guangdong responds to the opinions of the national development and Reform Commission. Primary school students incarnate as small community designers | national economic and Informa
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
根据csv文件某一列字符串中某个数字排序
升级 TiDB Operator
NFT smart contract release, blind box, public offering technology practice -- contract
【MySQL】日志
sys.argv
[Yugong series] February 2022 U3D full stack class 011 unity section 1 mind map
Upgrade tidb operator
使用 BR 恢复 S3 兼容存储上的备份数据
National economic information center "APEC industry +": economic data released at the night of the Spring Festival | observation of stable strategy industry fund
使用 TiDB Lightning 恢复 S3 兼容存储上的备份数据
使用 BR 备份 TiDB 集群数据到兼容 S3 的存储