-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn_queens.go
83 lines (73 loc) · 2.04 KB
/
n_queens.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package problem0051
/*
https://leetcode.com/problems/n-queens/
The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.
Given an integer n, return all distinct solutions to the n-queens puzzle. You may return the answer in any order.
Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space, respectively.
*/
func solveNQueens(n int) [][]string {
var results [][]string
var board = make([]string, n)
for idx := range board {
for i := 0; i < n; i++ {
board[idx] += "."
}
}
solveQueens(0, n, board, make([]int, n), make([]int, n), make([]int, n), &results)
return results
}
func solveQueens(cur, max int, board []string, cols, upDiags, downDiags []int, results *[][]string) {
if cur == max {
// If we reached the final stage this means we have a valid
// solution, so we copy the board to the results
final := make([]string, len(board))
copy(final, board)
*results = append(*results, final)
return
}
// Iterating over the possible places in the cur row
for i := 0; i < max; i++ {
var valid = true
// Checking if board[cur][i] is a valid position
// Checking collumns
for j := 0; j < cur; j++ {
if i == cols[j] {
valid = false
break
}
}
if !valid {
continue
}
// Checking upward diagonals
for j := 0; j < cur; j++ {
if cur-i == upDiags[j] {
valid = false
break
}
}
if !valid {
continue
}
// Checking downward diagonals
for j := 0; j < cur; j++ {
if cur+i == downDiags[j] {
valid = false
break
}
}
if !valid {
continue
}
// If we reached here, the space is valid
// We place a queen
board[cur] = board[cur][:i] + "Q" + board[cur][i+1:]
// We place the threatened cols and diags
cols[cur] = i
upDiags[cur] = cur - i
downDiags[cur] = cur + i
solveQueens(cur+1, max, board, cols, upDiags, downDiags, results)
// We remove the queen we place earlier
board[cur] = board[cur][:i] + "." + board[cur][i+1:]
}
}