Skip to content

Commit

Permalink
dir4 使用 pair
Browse files Browse the repository at this point in the history
  • Loading branch information
EndlessCheng committed May 8, 2020
1 parent ad97cf0 commit b2e730a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
24 changes: 13 additions & 11 deletions copypasta/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@ func commonCollection() {
pow10 := [...]int{1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9} // math.Pow10
factorial := [...]int{1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800 /*10!*/, 39916800, 479001600}
// TIPS: dir4[i] 和 dir4[i^1] 互为相反方向
dir4 := [...][2]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}} // 上下左右
dir4C := [...][2]int{ // 西东南北
type pair struct{ x, y int }
dir4 := [...]pair{{-1, 0}, {1, 0}, {0, -1}, {0, 1}} // 上下左右
dir4C := [...]pair{ // 西东南北
'W': {-1, 0},
'E': {1, 0},
'S': {0, -1},
'N': {0, 1},
}
dir4R := [...][2]int{{1, 1}, {-1, 1}, {-1, -1}, {1, -1}}
dir8 := [...][2]int{{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}
dir4R := [...]pair{{1, 1}, {-1, 1}, {-1, -1}, {1, -1}}
dir8 := [...]pair{{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}
orderP3 := [6][3]int{{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 0, 1}, {2, 1, 0}}

min := func(a, b int) int {
Expand Down Expand Up @@ -921,7 +922,6 @@ func monotoneCollection() {
}

func loopCollection() {
dir4 := [...][2]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}} // 上下左右
min := func(a, b int) int {
if a < b {
return a
Expand Down Expand Up @@ -983,13 +983,15 @@ func loopCollection() {
# #
#
*/
type pair struct{ x, y int }
dir4 := [...]pair{{-1, 0}, {1, 0}, {0, -1}, {0, 1}} // 上下左右
searchDir4 := func(maxI, maxJ, centerI, centerJ, dis int) {
for i, dir := range dir4 {
dir2 := dir4[(i+1)%4]
dx := dir2[0] - dir[0]
dy := dir2[1] - dir[1]
x := centerI + dir[0]*dis
y := centerJ + dir[1]*dis
for i, d := range dir4 {
d2 := dir4[(i+1)%4]
dx := d2.x - d.x
dy := d2.y - d.y
x := centerI + d.x*dis
y := centerJ + d.y*dis
for _i := 0; _i < dis; _i++ {
if x >= 0 && x < maxI && y >= 0 && y < maxJ {
// do
Expand Down
15 changes: 8 additions & 7 deletions copypasta/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package copypasta
import "sort"

func searchCollection() {
type _p struct{ x, y int }
dir4 := [...]_p{{-1, 0}, {1, 0}, {0, -1}, {0, 1}} // 上下左右

type point struct{ x, y int }

valid := func(g [][]byte, p point) bool {
Expand All @@ -14,7 +17,6 @@ func searchCollection() {
// 下列代码来自 LC162C https://leetcode-cn.com/problems/number-of-closed-islands/
// NOTE: 对于搜索格子的题,可以不用创建 vis 而是通过修改格子的值为范围外的值(如零、负数、'#' 等)来做到这一点
dfsGrids := func(g [][]byte) (comps int) {
dir4 := [...][2]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}} // 上下左右
n, m := len(g), len(g[0])
vis := make([][]bool, n)
for i := range vis {
Expand All @@ -34,8 +36,8 @@ func searchCollection() {
targetsPos = append(targetsPos, [2]int{i, j})
validComp := true
// 遍历完该连通分量再 return,保证不重不漏
for _, dir := range dir4 {
if !f(i+dir[0], j+dir[1]) {
for _, d := range dir4 {
if !f(i+d.x, j+d.y) {
validComp = false
}
}
Expand Down Expand Up @@ -84,7 +86,6 @@ func searchCollection() {

// 网格图从 (s.x,s.y) 到 (t.x,t.y) 的最短距离,'#' 为障碍物
// 无法到达时返回 -1
dir4 := [...][2]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}} // 上下左右
reachable := func(g [][]byte, s, t point) bool {
n, m := len(g), len(g[0])
vis := make([][]bool, n)
Expand All @@ -99,7 +100,7 @@ func searchCollection() {
return true
}
for _, d := range dir4 {
if xx, yy := p.x+d[0], p.y+d[1]; xx >= 0 && xx < n && yy >= 0 && yy < m && !vis[xx][yy] && g[xx][yy] != '#' {
if xx, yy := p.x+d.x, p.y+d.y; xx >= 0 && xx < n && yy >= 0 && yy < m && !vis[xx][yy] && g[xx][yy] != '#' {
vis[xx][yy] = true
q = append(q, point{xx, yy})
}
Expand All @@ -121,7 +122,7 @@ func searchCollection() {
return p.dep
}
for _, d := range dir4 {
if xx, yy := p.x+d[0], p.y+d[1]; xx >= 0 && xx < n && yy >= 0 && yy < m && !vis[xx][yy] && g[xx][yy] != '#' {
if xx, yy := p.x+d.x, p.y+d.y; xx >= 0 && xx < n && yy >= 0 && yy < m && !vis[xx][yy] && g[xx][yy] != '#' {
vis[xx][yy] = true
q = append(q, pair{point{xx, yy}, p.dep + 1})
}
Expand All @@ -143,7 +144,7 @@ func searchCollection() {
ps = append(ps, p)
}
for _, d := range dir4 {
if xx, yy := p.x+d[0], p.y+d[1]; xx >= 0 && xx < n && yy >= 0 && yy < m && !vis[xx][yy] && g[xx][yy] != '#' {
if xx, yy := p.x+d.x, p.y+d.y; xx >= 0 && xx < n && yy >= 0 && yy < m && !vis[xx][yy] && g[xx][yy] != '#' {
vis[xx][yy] = true
q = append(q, point{xx, yy})
}
Expand Down

0 comments on commit b2e730a

Please sign in to comment.