-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (50 loc) · 760 Bytes
/
main.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
func isSafe(m *[][]int8, row, col int) bool {
for i := 0; i < len(*m); i++ {
if (*m)[i][col] == 1 {
return false
}
}
var i, j int = row, col
for i >= 0 && j >= 0 {
if (*m)[i][j] == 1 {
return false
}
i--
j--
}
i, j = row, col
for i >= 0 && j < len(*m) {
if (*m)[i][j] == 1 {
return false
}
i--
j++
}
return true
}
func nQueen(m *[][]int8, row int) bool {
if row >= len(*m) {
return true
}
for i := 0; i < len(*m); i++ {
if isSafe(m, row, i) {
(*m)[row][i] = 1
if nQueen(m, row+1) {
return true
}
(*m)[row][i] = 0
}
}
return false
}
func main() {
example := [][]int8{
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
}
nQueen(&example, 0)
show(&example)
}