N-queens minimum and knights and bishops: Difference between revisions
N-queens minimum and knights and bishops (view source)
Revision as of 08:19, 13 May 2022
, 2 years ago→{{header|Go}}: Consolidated the two existing versions and tweaked a little more.
m (→Quick hack [temp]: coupla typos) |
(→{{header|Go}}: Consolidated the two existing versions and tweaked a little more.) |
||
Line 125:
=={{header|Go}}==
This was originally a translation of the Wren entry but was substantially improved by [[User:Petelomax|Pete Lomax]] using suggestions from the talk page and has been tweaked a little more since then.
A lot of time here is spent processing the 10 x 10 board for bishops. If bishops are limited to a 9 x 9 board, then the overall time falls to 31.4 seconds or, if one simply limits the placing of the bishops to a single row (or column) at or near the middle of the board, then 5 more seconds can be knocked off that.
Timings are for an Intel Core i7-8565U machine running Ubuntu 20.04.
<lang go>package main
Line 135 ⟶ 136:
"math"
"strings"
"time"
)
Line 154 ⟶ 156:
}
} else if piece == "B" {
if diag1Lookup[diag1[row][col]] || diag2Lookup[diag2[row][col]] {
return true
Line 192 ⟶ 191:
}
func
}
}
func
if piece == "Q" {
} else if piece == "B" {
} else { // piece == "K"
cd := abs(tcol -
return (rd == 1 && cd == 2) || (rd == 2 && cd == 1)
}
}
func storeLayout(piece string) {
var sb strings.Builder
Line 450 ⟶ 224:
layout = sb.String()
}
func placePiece(piece string, countSoFar, maxCount int) {
if countSoFar >= minCount {
return
Line 477 ⟶ 250:
return
}
if countSoFar <= maxCount {
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
if !isAttacked(piece, i, j) {
if (i == ti && j == tj) ||
board[i][j] = true
}
placePiece(piece, countSoFar+1, maxCount)
board[i][j] = false
diag2Lookup[diag2[i][j]] = false
}
}
}
Line 497 ⟶ 272:
}
}
func main() {
start := time.Now()
pieces := []string{"Q", "B", "K"}
limits := map[string]int{"Q": 10, "B":
names := map[string]string{"Q": "Queens", "B": "Bishops", "K": "Knights"}
for _, piece := range pieces {
fmt.Println(names[piece])
fmt.Println("=======\n")
for n = 1; ; n++ {
board = make([][]bool, n)
for i := 0; i < n; i++ {
board[i] = make([]bool, n)
}
diag1[i][j] = i + j
}
}
}
diag1Lookup = make([]bool, 2*n-1)
diag2Lookup = make([]bool, 2*n-1)
}
minCount = math.MaxInt32
layout = ""
for maxCount := 1; maxCount <= n*n; maxCount++ {
placePiece(piece, 0, maxCount)
if minCount <= n*n {
break
}
Line 547 ⟶ 322:
}
elapsed := time.Now().Sub(start)
fmt.Printf("
}</lang>
{{out}}
<pre>
Line 567 ⟶ 343:
Queens on a 10 x 10 board:
. . Q . . . . . . .
. . . . . . . . . .
. . . . . . . . Q .
. . . . . . . . . .
. . . . Q . . . . .
. . . . . . . . . .
Q . . . . . . . . .
. . . . . . . . . .
. . . . . . Q . . .
. . . . . . . . . .
Bishops
Line 590 ⟶ 366:
8 x 8 : 8
9 x 9 : 9
10 x 10 : 10
Bishops on a
B .
. . . . .
. . . B
. . . . . . . . . .
. . . . . . . . . .
. . .
. . . .
. . .
. . . .
. . . . . . . . . .
Knights
Line 619 ⟶ 397:
Knights on a 10 x 10 board:
. . . . . . . . . .
. . K K . . . . . .
. . K K . . . K K .
. . . . . . . K K .
. . . . . . . . . .
. . . . . . . . . .
. K K . . . . . . .
. K K . . . K K . .
. . . . . . K K . .
. . . . . . . . . .
Took 1m48.35390296s
</pre>
|