N-queens minimum and knights and bishops: Difference between revisions

Content added Content deleted
(→‎{{header|Go}}: Consolidated the two existing versions and tweaked a little more.)
(→‎{{header|Go}}: Further speed up, about 4 times quicker than before.)
Line 125: Line 125:


=={{header|Go}}==
=={{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.
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 improved further since then, resulting in an overall execution time of about 28.2 seconds.


Timing is for an Intel Core i7-8565U machine running Ubuntu 20.04.
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
<lang go>package main


Line 249: Line 247:
storeLayout(piece)
storeLayout(piece)
return
return
}
si := 0
sj := 0
if piece != "K" {
si = ti
sj = tj
}
}
if countSoFar <= maxCount {
if countSoFar <= maxCount {
for i := 0; i < n; i++ {
for i := si; i < n; i++ {
for j := 0; j < n; j++ {
for j := sj; j < n; j++ {
if !isAttacked(piece, i, j) {
if !isAttacked(piece, i, j) {
if (i == ti && j == tj) || attacks(piece, i, j, ti, tj) {
if (i == ti && j == tj) || attacks(piece, i, j, ti, tj) {
Line 370: Line 374:
Bishops on a 10 x 10 board:
Bishops on a 10 x 10 board:


B . . . . . . . . B
. . . . . . . . . B
. . . . . . . . . .
. . . B B B B . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . B B . . . .
. . . B . B . . . .
. . . B . B . B . .
B . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . B B . . . .
. . . . . B . . . .
. . . . . B . . . .
. . . . . B . . . .
. . . . . . . . . .
. . . . . . . . . .


Line 408: Line 412:
. . . . . . . . . .
. . . . . . . . . .


Took 1m48.35390296s
Took 28.201643739s
</pre>
</pre>