-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtotal_queens.go
69 lines (60 loc) · 1.45 KB
/
total_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
package problem0052
/*
https://leetcode.com/problems/n-queens-ii/
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 the number of distinct solutions to the n-queens puzzle.
*/
func totalNQueens(n int) int {
var results int
solveQueens(0, n, make([]int, n), make([]int, n), make([]int, n), &results)
return results
}
func solveQueens(cur, max int, cols, upDiags, downDiags []int, results *int) {
if cur == max {
// If we reached the final stage this means we have a valid
// solution, so we copy the board to the results
*results++
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 the threatened cols and diags
cols[cur] = i
upDiags[cur] = cur - i
downDiags[cur] = cur + i
solveQueens(cur+1, max, cols, upDiags, downDiags, results)
}
}