1234567891011121314151617181920212223242526272829303132 |
- package main
- import (
- "fmt"
- )
- func solveNQueens(n int) [][]string {
- solutions := make([][]string, 0)
- colNum := make([]int, n)
- colLaid := make([]bool, n)
- mainLaid := make([]bool, 2*n) // Range of row-col is [1-n, n-1], so row-col+n -> [1, 2n-1]
- viceLaid := make([]bool, 2*n)
- for row := 0; row < n; row++ { // The ith queen
- for col := 0; col < n; col++ {
- idx := row - col + n
- if colLaid[col] || mainLaid[idx] || viceLaid[idx] {
- continue
- }
- colNum[row] = col
- colLaid[col] = true
- mainLaid[idx] = true
- viceLaid[idx] = true
- break
- }
- }
- fmt.Println(colNum)
- return solutions
- }
- // func main() {
- // fmt.Println(solveNQueens(4))
- // }
|