-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn-queens-ii.go
58 lines (48 loc) · 975 Bytes
/
n-queens-ii.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
package main
import (
"fmt"
)
// source: https://leetcode.com/problems/n-queens-ii/
func contains(s []int, e int) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func totalNQueens(n int) int {
var sol = make([][]int, 0)
var diff = make(map[int]bool)
var sum = make(map[int]bool)
var queens = make([]int, 0)
var util func(qs []int, d, s map[int]bool)
util = func(qs []int, d, s map[int]bool) {
p := len(qs)
if p == n {
tmp := make([]int, n)
copy(tmp, qs)
sol = append(sol, tmp)
return
}
for q := 0; q < n; q++ {
if !contains(qs, q) && !d[p-q] && !s[p+q] {
d[p-q] = true
s[p+q] = true
util(append(qs, q), d, s)
d[p-q] = false
s[p+q] = false
}
}
}
util(queens, diff, sum)
return len(sol)
}
func main() {
// Example 1
var n1 int = 4
fmt.Println("Expected: 2 Output: ", totalNQueens(n1))
// Example 2
var n2 int = 1
fmt.Println("Expected: 1 Output: ", totalNQueens(n2))
}