golang 数组中的元素被未知原因修改

```go func solveNQueens(n int) [][]string { var result [][]int queueRows := make(map[int]struct{}) queuePies := make(map[int]struct{}) queueNas := make(map[int]struct{}) var backtrace func(row int, cur []int) backtrace = func(row int, cur []int) { fmt.Println(result) if row >= n { result = append(result, cur) fmt.Println(cur) fmt.Println(result) } for col := 0; col < n; col++ { _, ok1 := queueRows[col] _, ok2 := queuePies[row+col] _, ok3 := queueNas[row-col] if ok1 || ok2 || ok3 { continue } queueRows[col] = struct{}{} queueNas[row-col] = struct{}{} queuePies[row+col] = struct{}{} backtrace(row+1, append(cur, col)) delete(queueRows, col) delete(queueNas, row-col) delete(queuePies, row+col) } } backtrace(0, []int{}) return output(result, n) } func output(result [][]int, n int) [][]string { var r [][]string for _, v := range result { var s []string for _, pos := range v { s = append(s, strings.Repeat(".", pos)+"Q"+strings.Repeat(".", n-1-pos)) } r = append(r, s) } return r } func main() { solveNQueens(7) } ``` 代码如上所示,使用递归的方式解决N皇后问题,但是在N大于7时会出现一个奇怪的错误,result的值自动发生了改变,改变时的打印如下所示: 前一次打印:`[[0 2 4 6 1 3 5] [0 3 6 2 5 1 4] [0 4 1 5 2 6 3] [0 5 3 1 6 4 2] [1 3 0 6 4 2 5] [1 3 5 0 2 4 6] [1 4 0 3 6 2 5] [1 4 2 0 6 3 5] [1 4 6 3 0 2 5] [1 5 2 6 3 0 4] [1 6 4 2 0 5 3] [2 0 5 1 4 6 3] [2 0 5 3 1 6 4] [2 4 6 1 3 5 0] [2 5 1 4 0 3 6] [2 6 1 3 5 0 4] [2 6 3 0 4 1 5] [3 0 2 5 1 6 4] [3 0 4 1 5 2 6] [3 1 6 4 2 0 5] [3 5 0 2 4 6 1] [3 6 2 5 1 4 0] [3 6 4 1 5 0 2] [4 0 3 6 2 5 1] [4 0 5 3 1 6 2] [4 1 5 2 6 3 0] [4 2 0 5 3 1 6] [4 6 1 3 5 0 2] [4 6 1 5 2 0 3] [5 0 2 4 6 3 3] [5 1 4 0 3 6 2] [5 2 0 3 6 4 1] [5 2 4 6 0 3 1] [5 2 6 3 0 4 1] [5 3 1 6 4 2 0] [5 3 6 0 2 4 1] [6 1 3 5 0 2 4]]` 后一次打印: `[[0 2 4 6 1 3 5] [0 3 6 2 5 1 4] [0 4 1 5 2 6 3] [0 5 3 1 6 4 2] [1 3 0 6 4 2 5] [1 3 5 0 2 4 6] [1 4 0 3 6 2 5] [1 4 2 0 6 3 5] [1 4 6 3 0 2 5] [1 5 2 6 3 0 4] [1 6 4 2 0 5 3] [2 0 5 1 4 6 3] [2 0 5 3 1 6 4] [2 4 6 1 3 5 0] [2 5 1 4 0 3 6] [2 6 1 3 5 0 4] [2 6 3 0 4 1 5] [3 0 2 5 1 6 4] [3 0 4 1 5 2 6] [3 1 6 4 2 0 5] [3 5 0 2 4 6 1] [3 6 2 5 1 4 0] [3 6 4 1 5 0 2] [4 0 3 6 2 5 1] [4 0 5 3 1 6 2] [4 1 5 2 6 3 0] [4 2 0 5 3 1 6] [4 6 1 3 5 0 2] [4 6 1 5 2 0 3] [5 0 2 4 6 3 3] [5 1 4 0 3 6 2] [5 2 0 3 6 4 1] [5 2 4 6 0 3 1] [5 2 6 3 0 4 1] [5 3 1 6 4 2 0] [5 3 6 0 2 4 1] [6 1 3 5 0 4 4]]` 差别在最后一个值,从`[6 1 3 5 0 2 4]`变成了` [6 1 3 5 0 4 4]`,但代码中没有任何对result的单值修改操作,仅有append操作,感觉很是疑惑,是否有大神帮忙看一下? 附上N皇后问题的描述: https://leetcode.com/problems/n-queens/submissions/
已邀请:

要回复问题请先登录注册