-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8_queens.go
150 lines (112 loc) · 3.05 KB
/
8_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"fmt"
"sync"
"time"
)
const (
// QUEEN 皇后的个数
QUEEN = 16
)
// wg 计数器
var wg sync.WaitGroup
func calcChessWhenFirstRowIs(firstRow int, result chan int64) {
// QueenPos 用来记录列里面哪些已经放了queen,不用再放
var QueenPos uint64 = 0x00000000
// QueenLeftPos 用来记录左斜里面放了,n行,m列,对应 n+m 位
var QueenLeftPos uint64 = 0x0000000000000000
// QueenRightPos 用来记录右斜里面放了,n行,m列,对应 Q - m + n - 1 位
var QueenRightPos uint64 = 0x0000000000000000
var oneBit uint64 = 0x0000000000000001
defer wg.Done()
// 整个棋盘
var chess [QUEEN]int
// 初始化
chess[0] = firstRow
QueenPos = oneBit << uint(firstRow)
QueenLeftPos = oneBit << uint(firstRow)
QueenRightPos = oneBit << uint(QUEEN-1-firstRow)
var ii int
for ii = 1; ii < QUEEN; ii++ {
chess[ii] = -1
}
var row int = 1
var s int64
for {
if row == 0 {
break
}
var origin = chess[row]
var column = origin
var pos, leftPos, rightPos uint64
var originLeft = row + origin
var originRight = QUEEN - 1 + row - origin
var ok bool
var result bool = true
for ok = true; ok; ok = (QueenPos&pos) > 0 || (QueenLeftPos&leftPos) > 0 || (QueenRightPos&rightPos) > 0 {
column = column + 1
if column >= QUEEN {
// 已经是最后一个位置,超过了
chess[row] = -1
if origin != -1 { // 清除原来的位置
QueenPos = QueenPos & ^(oneBit << uint(origin))
QueenLeftPos = QueenLeftPos & ^(oneBit << uint(originLeft))
QueenRightPos = QueenRightPos & ^(oneBit << uint(originRight))
}
result = false
break
}
pos = oneBit << uint(column)
leftPos = oneBit << uint(row+column)
rightPos = oneBit << uint(QUEEN-1+row-column)
}
if result == true {
chess[row] = column
QueenPos = QueenPos | (oneBit << uint(column))
QueenLeftPos = QueenLeftPos | (oneBit << uint(column+row))
QueenRightPos = QueenRightPos | (oneBit << uint(QUEEN-1+row-column))
if origin != -1 { // 清除原来的位置
QueenPos = QueenPos & ^(oneBit << uint(origin))
QueenLeftPos = QueenLeftPos & ^(oneBit << uint(originLeft))
QueenRightPos = QueenRightPos & ^(oneBit << uint(originRight))
}
} else {
row--
continue
}
if row == QUEEN-1 {
s++
var pos = chess[row]
QueenPos = QueenPos & ^(oneBit << uint(pos))
QueenLeftPos = QueenLeftPos & ^(oneBit << uint(QUEEN-1+pos))
QueenRightPos = QueenRightPos & ^(oneBit << uint(QUEEN-1+QUEEN-1-pos))
chess[row] = -1
row--
} else {
row++
}
}
result <- s
}
func main() {
fmt.Printf("Calculating %d queens...\n", QUEEN)
var now = time.Now().UnixNano()
var sizes chan int64 = make(chan int64)
var i int
for i = 0; i < QUEEN; i++ {
wg.Add(1)
go calcChessWhenFirstRowIs(i, sizes)
}
// closer
go func() {
wg.Wait()
close(sizes)
}()
var size, total int64
for size = range sizes {
total += size
}
var afterCaculation = time.Now().UnixNano()
fmt.Printf("There is total %d solutions \n", total)
fmt.Println((afterCaculation-now)/1000, "us")
}