[Fix] 获取一个连续位置的矩阵中,特定位置相邻的最多四个方向的位置时,左右两方可能会换行的问题

This commit is contained in:
kercylan98 2023-06-12 17:43:51 +08:00
parent 8cc6aa879f
commit b11be611e2
1 changed files with 3 additions and 2 deletions

View File

@ -36,16 +36,17 @@ func GetAdjacentPositions[T any](matrix [][]T, x, y int) (result [][2]int) {
// GetAdjacentPositionsWithContinuousPosition 获取一个连续位置的矩阵中,特定位置相邻的最多四个方向的位置
func GetAdjacentPositionsWithContinuousPosition[T any](matrix []T, width, pos int) (result []int) {
size := len(matrix)
currentRow := pos / width
if up := pos - width; up >= 0 {
result = append(result, up)
}
if down := pos + width; down < size {
result = append(result, down)
}
if left := pos - 1; pos >= 0 {
if left := pos - 1; left >= 0 && currentRow == (left/width) {
result = append(result, left)
}
if right := pos + 1; right < size {
if right := pos + 1; right < size && currentRow == (right/width) {
result = append(result, right)
}
return