diff --git a/copypasta/common.go b/copypasta/common.go index 1fa98ffd75..6afaccf4fe 100644 --- a/copypasta/common.go +++ b/copypasta/common.go @@ -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 { @@ -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 @@ -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 diff --git a/copypasta/search.go b/copypasta/search.go index 465956fccc..0c57990525 100644 --- a/copypasta/search.go +++ b/copypasta/search.go @@ -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 { @@ -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 { @@ -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 } } @@ -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) @@ -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}) } @@ -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}) } @@ -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}) }