邓心一 6 anni fa
parent
commit
5d68af6ddc
2 ha cambiato i file con 71 aggiunte e 47 eliminazioni
  1. 27 27
      hard/37.go
  2. 44 20
      hard/51.go

+ 27 - 27
hard/37.go

@@ -98,31 +98,31 @@ func solveSudoku(board [][]byte) {
 	}
 }
 
-func main() {
-	b1 := [][]byte{
-		{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
-		{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
-		{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
-		{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
-		{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
-		{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
-		{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
-		{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
-		{'.', '.', '.', '.', '8', '.', '.', '7', '9'}}
-	solveSudoku(b1)
-	printBoard(b1)
+// func main() {
+// 	b1 := [][]byte{
+// 		{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
+// 		{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
+// 		{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
+// 		{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
+// 		{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
+// 		{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
+// 		{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
+// 		{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
+// 		{'.', '.', '.', '.', '8', '.', '.', '7', '9'}}
+// 	solveSudoku(b1)
+// 	printBoard(b1)
 
-	println()
-	b2 := [][]byte{
-		{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
-		{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
-		{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
-		{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
-		{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
-		{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
-		{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
-		{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
-		{'.', '.', '.', '.', '.', '.', '.', '.', '.'}}
-	solveSudoku(b2)
-	printBoard(b2)
-}
+// 	println()
+// 	b2 := [][]byte{
+// 		{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
+// 		{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
+// 		{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
+// 		{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
+// 		{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
+// 		{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
+// 		{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
+// 		{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
+// 		{'.', '.', '.', '.', '.', '.', '.', '.', '.'}}
+// 	solveSudoku(b2)
+// 	printBoard(b2)
+// }

+ 44 - 20
hard/51.go

@@ -2,31 +2,55 @@ package main
 
 import (
 	"fmt"
+	"strings"
 )
 
-func solveNQueens(n int) [][]string {
-	solutions := make([][]string, 0)
-	colNum := make([]int, n)
+func solveNQueens(n int) (solutions [][]string) {
+	putX := 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
+	mainLaid := make([]bool, 2*n) // Range of y-x is [1-n, n-1], so y-x+n -> [1, 2n-1]
+	viceLaid := make([]bool, 2*n-1)
+	for y, x := 0, 0; ; { // The ith queen
+		for ; x < n; x++ {
+			if !(colLaid[x] || mainLaid[y-x+n] || viceLaid[x+y]) {
+				putX[y] = x
+				colLaid[x] = true
+				mainLaid[y-x+n] = true
+				viceLaid[x+y] = true
+				x = 0 // Next step
+				y++
+				break
 			}
-			colNum[row] = col
-			colLaid[col] = true
-			mainLaid[idx] = true
-			viceLaid[idx] = true
-			break
+		}
+		if x == n || y == n { // Not valid or finished
+			if y == 0 { // If y is 0 and x is n, then all solutions are found
+				return
+			} else if y == n {
+				solutions = append(solutions, make([]string, 0))
+				l := len(solutions)
+				var sb strings.Builder
+				for i := 0; i < n; i++ {
+					sb.Reset()
+					for j := 0; j < n; j++ {
+						if putX[i] == j {
+							sb.WriteByte('Q')
+						} else {
+							sb.WriteByte('.')
+						}
+					}
+					solutions[l-1] = append(solutions[l-1], sb.String())
+				}
+			}
+			y-- // Back tracking
+			x = putX[y]
+			colLaid[x] = false
+			mainLaid[y-x+n] = false
+			viceLaid[x+y] = false
+			x++
 		}
 	}
-	fmt.Println(colNum)
-	return solutions
 }
 
-// func main() {
-// 	fmt.Println(solveNQueens(4))
-// }
+func main() {
+	fmt.Println(solveNQueens(9))
+}