/
queen8.go
58 lines (52 loc) · 1.16 KB
/
queen8.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"
)
const (
size = 8
norm = size - 1
)
var B = [size][size]string{
{".", ".", ".", ".", ".", ".", ".", "."},
{".", ".", ".", ".", ".", ".", ".", "."},
{".", ".", ".", ".", ".", ".", ".", "."},
{".", ".", ".", ".", ".", ".", ".", "."},
{".", ".", ".", ".", ".", ".", ".", "."},
{".", ".", ".", ".", ".", ".", ".", "."},
{".", ".", ".", ".", ".", ".", ".", "."},
{".", ".", ".", ".", ".", ".", ".", "."}}
var leftdiag [size*2 - 1]bool
var rightdiag [size*2 - 1]bool
var col [size]bool
func main() {
tryQueen(0)
}
func tryQueen(x int) {
for y := 0; y < size; y++ {
if col[y] == false && leftdiag[x-y+norm] == false && rightdiag[x+y] == false {
B[y][x] = "Q" // reverse coordinates so it prints correctly
col[y] = true
leftdiag[x-y+norm] = true
rightdiag[x+y] = true
if x < size-1 {
tryQueen(x + 1)
} else {
fmt.Printf("Solution found\n%s", func() string {
s := ""
for i := 0; i < 8; i++ {
for j := 0; j < 8; j++ {
s += B[i][j]
}
s += "\n"
}
return s
}())
return
}
B[y][x] = "."
col[y] = false
leftdiag[x-y+norm] = false
rightdiag[x+y] = false
}
}
}