邓心一 преди 6 години
родител
ревизия
7ba642b8a3
променени са 1 файла, в които са добавени 32 реда и са изтрити 0 реда
  1. 32 0
      hard/51.go

+ 32 - 0
hard/51.go

@@ -0,0 +1,32 @@
+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))
+// }