Skip to content

Commit

Permalink
update dfsGrids
Browse files Browse the repository at this point in the history
  • Loading branch information
EndlessCheng committed May 1, 2020
1 parent 1627388 commit 3bcd75c
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions copypasta/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,35 @@ func searchCollection() {
for i := range vis {
vis[i] = make([]bool, m)
}
const target byte = '.'
var targetsPos [][2]int
var f func(i, j int) bool
f = func(i, j int) bool {
// 出边界的不算
if i < 0 || i >= n || j < 0 || j >= m {
return false
}
if vis[i][j] || g[i][j] != 0 {
} // 出边界的不算
if vis[i][j] || g[i][j] != target {
return true
}
vis[i][j] = true
targetsPos = append(targetsPos, [2]int{i, j})
validComp := true
// 遍历完该连通分量再 return,保证不重不漏
for _, dir := range dir4 {
if !f(i+dir[0], j+dir[1]) {
// 遍历完该连通分量再 return,保证不重不漏
validComp = false
}
}
return validComp
}
for i, gi := range g {
for j, gij := range gi {
if gij == 0 && !vis[i][j] && f(i, j) {
comps++
if gij == target && !vis[i][j] {
targetsPos = [][2]int{}
if f(i, j) {
comps++
// do targetsPos...
}
}
}
}
Expand Down

0 comments on commit 3bcd75c

Please sign in to comment.