package Matrix
// 时间复杂度: O(n * m), 空间复杂度: O(n * m)
func spiralOrder(matrix [][]int) []int {
n, m := len(matrix), len(matrix[0])
visited := make([][]bool, n)
for i := 0; i < n; i++ {
visited[i] = make([]bool, m)
}
move := [4][2]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
curX, curY, curMove := 0, 0, 0
result := make([]int, 0)
for k := 0; k < n*m; k++ {
result = append(result, matrix[curX][curY])
visited[curX][curY] = true
nextX := curX + move[curMove][0]
nextY := curY + move[curMove][1]
if nextX >= 0 && nextX < n && nextY >= 0 && nextY < m && !visited[nextX][nextY] {
curX, curY = nextX, nextY
} else {
curMove = (curMove + 1) % 4
nextX = curX + move[curMove][0]
nextY = curY + move[curMove][1]
curX, curY = nextX, nextY
}
}
return result
}