Peaceful chess queen armies: Difference between revisions

m
→‎{{header|jq}}: add newline
m (→‎{{header|jq}}: add newline)
 
(9 intermediate revisions by 6 users not shown)
Line 58:
{{trans|D}}
 
<langsyntaxhighlight lang="11l">T.enum Piece
EMPTY
BLACK
Line 138:
printBoard(nm[0], blackQueens, whiteQueens)
E
print("No solution exists.\n")</langsyntaxhighlight>
 
{{out}}
Line 314:
The program also can stop after printing a specified number of solutions, although the default is to print all solutions.
 
(Commentary by the author: this program suffers similarly of slowness, in eliminating rotational equivalents, as does its Scheme ancestor. Some reasons: it uses backtracking and that is slow; it uses essentially the same inefficient storage format for solutions [one could for instance use integers], and it does not precompute rotational equivalents. However, it does satisfy the task requirements, and might be regarded as a good start. And it is can solve the m=5, n=6 case in practical time on a fast machine. m=7, n=7 is a more annoying case.)
 
<langsyntaxhighlight lang="ats">(********************************************************************)
 
#define ATS_DYNLOADFLAG 0
Line 1,260:
end
 
(********************************************************************)</langsyntaxhighlight>
 
{{out}}
Line 1,306:
=={{header|C}}==
{{trans|C#}}
<langsyntaxhighlight lang="c">#include <math.h>
#include <stdbool.h>
#include <stdio.h>
Line 1,578:
 
return EXIT_SUCCESS;
}</langsyntaxhighlight>
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 1,745:
=={{header|C sharp|C#}}==
{{trans|D}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
 
Line 1,858:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 2,025:
=={{header|C++}}==
{{trans|D}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <vector>
 
Line 2,139:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 2,306:
=={{header|D}}==
{{trans|Go}}
<langsyntaxhighlight lang="d">import std.array;
import std.math;
import std.stdio;
Line 2,417:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 2,584:
=={{header|Fortran}}==
{{works with|gfortran|11.2.1}}
 
The example demonstrates modern Fortran’s capabilities for integer bit manipulation, by using large machine integers (and their entire bitrange) as bitmaps to represent queen armies. Complicated (but nevertheless single-statement) expressions of such integers represent such operations as rotating a chessboard and checking for any attacks.
 
There are two Fortran programs and a driver script. One program generates a Fortran module for basic operations; the other program (which must be linked with the generated module) does the actual work. The driver script is for Unix shell.
 
Line 2,589 ⟶ 2,592:
 
Here is the first program, '''peaceful_queens_elements_generator.f90''', which generates code (specialized for given m and n) to deal with the representations of the armies as integers:
<langsyntaxhighlight lang="fortran">program peaceful_queens_elements_generator
use, intrinsic :: iso_fortran_env, only: int64
use, intrinsic :: iso_fortran_env, only: error_unit
Line 3,404 ⟶ 3,407:
end subroutine fill_queen_masks
 
end program peaceful_queens_elements_generator</langsyntaxhighlight>
 
Here is the second program, '''peaceful_queens.f90''':
<langsyntaxhighlight lang="fortran">module peaceful_queens_support
use, non_intrinsic :: peaceful_queens_elements
 
Line 3,719 ⟶ 3,722:
& num_solutions, armies1, armies2)
 
end program peaceful_queens</langsyntaxhighlight>
 
Here is the driver script:
<langsyntaxhighlight lang="sh">#!/bin/sh
#
# Driver script for peaceful_queens in Fortran.
Line 3,766 ⟶ 3,769:
${FC} ${FCFLAGS} -c peaceful_queens.f90 &&
${FC} ${FCFLAGS} -o peaceful_queens peaceful_queens_elements.o peaceful_queens.o &&
if test x"${RUN_IT}" = xyes; then time ./peaceful_queens ${SHOW_EQUIVALENTS}; else :; fi</langsyntaxhighlight>
 
{{out}}
Line 3,996 ⟶ 3,999:
 
It would be instructive to save and examine the generated '''peaceful_queens_elements.f90''' files. I leave that as an exercise for the reader. :)
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">Type posicion
x As Integer
y As Integer
End Type
 
Type pieza
empty As Integer
black As Integer
white As Integer
End Type
 
Function isAttacking(q As posicion, posic As posicion) As Integer
Return (q.x = posic.x Or q.y = posic.y Or Abs(q.x - posic.x) = Abs(q.y - posic.y))
End Function
 
Sub place(m As Integer, n As Integer, blackQueens() As posicion, whiteQueens() As posicion, Byref result As Integer)
If m = 0 Then
result = -1
Exit Sub
End If
Dim As Integer placingBlack = -1
Dim As Integer i, j, k, equalposicion
Dim As Boolean inner
For i = 0 To n-1
For j = 0 To n-1
Dim As posicion posic = Type<posicion>(i, j)
inner = False
For k = Lbound(blackQueens) To Ubound(blackQueens)
equalposicion = (blackQueens(k).x = posic.x And blackQueens(k).y = posic.y)
If equalposicion Or (Not placingBlack And isAttacking(blackQueens(k), posic)) Then
inner = True
Exit For
End If
Next
If Not inner Then
For k = Lbound(whiteQueens) To Ubound(whiteQueens)
equalposicion = (whiteQueens(k).x = posic.x And whiteQueens(k).y = posic.y)
If equalposicion Or (placingBlack And isAttacking(whiteQueens(k), posic)) Then
inner = True
Exit For
End If
Next
If Not inner Then
If placingBlack Then
Redim Preserve blackQueens(Ubound(blackQueens) + 1)
blackQueens(Ubound(blackQueens)) = posic
placingBlack = 0
Else
Redim Preserve whiteQueens(Ubound(whiteQueens) + 1)
whiteQueens(Ubound(whiteQueens)) = posic
place(m-1, n, blackQueens(), whiteQueens(), result)
If result Then Exit Sub
Redim Preserve blackQueens(Ubound(blackQueens) - 1)
Redim Preserve whiteQueens(Ubound(whiteQueens) - 1)
placingBlack = -1
End If
End If
End If
Next
Next
If Not placingBlack Then Redim Preserve blackQueens(Ubound(blackQueens) - 1)
result = 0
End Sub
 
Sub printBoard(n As Integer, blackQueens() As posicion, whiteQueens() As posicion)
Dim As Integer board(n * n)
Dim As Integer i, j, k
For i = Lbound(blackQueens) To Ubound(blackQueens)
board(blackQueens(i).x * n + blackQueens(i).y) = 1
Next
For i = Lbound(whiteQueens) To Ubound(whiteQueens)
board(whiteQueens(i).x * n + whiteQueens(i).y) = 2
Next
For i = 0 To n*n-1
If i Mod n = 0 And i <> 0 Then Print
Select Case board(i)
Case 1
Print "B ";
Case 2
Print "W ";
Case Else
j = i \ n
k = i - j * n
If j Mod 2 = k Mod 2 Then
Print Chr(253); " ";
Else
Print Chr(252); " ";
End If
End Select
Next i
Print
End Sub
 
Dim As posicion nms(23) = { _
Type<posicion>(2, 1), Type<posicion>(3, 1), Type<posicion>(3, 2), Type<posicion>(4, 1), Type<posicion>(4, 2), Type<posicion>(4, 3), _
Type<posicion>(5, 1), Type<posicion>(5, 2), Type<posicion>(5, 3), Type<posicion>(5, 4), Type<posicion>(5, 5), _
Type<posicion>(6, 1), Type<posicion>(6, 2), Type<posicion>(6, 3), Type<posicion>(6, 4), Type<posicion>(6, 5), Type<posicion>(6, 6), _
Type<posicion>(7, 1), Type<posicion>(7, 2), Type<posicion>(7, 3), Type<posicion>(7, 4), Type<posicion>(7, 5), Type<posicion>(7, 6), Type<posicion>(7, 7) }
 
For i As Integer = Lbound(nms) To Ubound(nms)
Print Chr(10); nms(i).y; " black and "; nms(i).y; " white queens on a "; nms(i).x; " x "; nms(i).x; " board:"
Dim As posicion blackQueens(0)
Dim As posicion whiteQueens(0)
Dim As Integer result
place(nms(i).y, nms(i).x, blackQueens(), whiteQueens(), result)
If result Then
printBoard(nms(i).x, blackQueens(), whiteQueens())
Else
Print "No solution exists."
End If
Next i
 
Sleep</syntaxhighlight>
{{out}}
<pre>
1 black and 1 white queens on a 2 x 2 board:
No solution exists.
 
1 black and 1 white queens on a 3 x 3 board:
² ³ ²
³ ² B
W ³ ²
 
2 black and 2 white queens on a 3 x 3 board:
² B ²
³ ² ³
² ³ W
 
1 black and 1 white queens on a 4 x 4 board:
² ³ ² ³
³ ² B ²
W ³ ² ³
³ ² ³ ²
 
2 black and 2 white queens on a 4 x 4 board:
² B ² ³
³ ² ³ W
² ³ ² ³
³ ² ³ ²
 
3 black and 3 white queens on a 4 x 4 board:
B ³ ² ³
³ ² W ²
² ³ ² ³
³ ² ³ ²
 
1 black and 1 white queens on a 5 x 5 board:
² ³ ² ³ ²
³ ² B ² ³
W ³ ² ³ ²
³ ² ³ ² ³
² ³ ² ³ ²
 
2 black and 2 white queens on a 5 x 5 board:
² B ² ³ ²
³ ² ³ W ³
² ³ ² ³ ²
³ ² ³ ² ³
² ³ ² ³ ²
 
3 black and 3 white queens on a 5 x 5 board:
B ³ ² ³ ²
³ ² W ² ³
² ³ ² ³ ²
³ ² ³ ² ³
² ³ ² ³ ²
 
4 black and 4 white queens on a 5 x 5 board:
² ³ ² ³ B
W ² ³ ² ³
² ³ ² ³ ²
³ ² ³ ² ³
² ³ ² ³ ²
 
5 black and 5 white queens on a 5 x 5 board:
² ³ B ³ ²
³ ² ³ ² W
² ³ ² ³ ²
³ ² ³ ² ³
² ³ ² ³ ²
 
1 black and 1 white queens on a 6 x 6 board:
² ³ ² ³ ² ³
³ ² B ² ³ ²
W ³ ² ³ ² ³
³ ² ³ ² ³ ²
² ³ ² ³ ² ³
³ ² ³ ² ³ ²
 
2 black and 2 white queens on a 6 x 6 board:
² B ² ³ ² ³
³ ² ³ W ³ ²
² ³ ² ³ ² ³
³ ² ³ ² ³ ²
² ³ ² ³ ² ³
³ ² ³ ² ³ ²
 
3 black and 3 white queens on a 6 x 6 board:
B ³ ² ³ ² ³
³ ² W ² ³ ²
² ³ ² ³ ² ³
³ ² ³ ² ³ ²
² ³ ² ³ ² ³
³ ² ³ ² ³ ²
 
4 black and 4 white queens on a 6 x 6 board:
² ³ ² ³ B ³
W ² ³ ² ³ ²
² ³ ² ³ ² ³
³ ² ³ ² ³ ²
² ³ ² ³ ² ³
³ ² ³ ² ³ ²
 
5 black and 5 white queens on a 6 x 6 board:
² ³ B ³ ² ³
³ ² ³ ² W ²
² ³ ² ³ ² ³
³ ² ³ ² ³ ²
² ³ ² ³ ² ³
³ ² ³ ² ³ ²
 
6 black and 6 white queens on a 6 x 6 board:
B ³ ² ³ ² ³
³ ² W ² ³ ²
² ³ ² ³ ² ³
³ ² ³ ² ³ ²
² ³ ² ³ ² ³
³ ² ³ ² ³ ²
 
1 black and 1 white queens on a 7 x 7 board:
² ³ ² ³ ² ³ ²
³ ² B ² ³ ² ³
W ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
 
2 black and 2 white queens on a 7 x 7 board:
² B ² ³ ² ³ ²
³ ² ³ W ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
 
3 black and 3 white queens on a 7 x 7 board:
B ³ ² ³ ² ³ ²
³ ² W ² ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
 
4 black and 4 white queens on a 7 x 7 board:
² ³ ² ³ B ³ ²
W ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
 
5 black and 5 white queens on a 7 x 7 board:
² ³ B ³ ² ³ ²
³ ² ³ ² W ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
 
6 black and 6 white queens on a 7 x 7 board:
B ³ ² ³ ² ³ ²
³ ² W ² ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
 
7 black and 7 white queens on a 7 x 7 board:
² ³ ² ³ B ³ ²
W ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²
³ ² ³ ² ³ ² ³
² ³ ² ³ ² ³ ²</pre>
 
=={{header|Go}}==
Line 4,001 ⟶ 4,299:
 
Textual rather than HTML output. Whilst the unicode symbols for the black and white queens are recognized by the Ubuntu 16.04 terminal, I found it hard to visually distinguish between them so I've used 'B' and 'W' instead.
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 4,125 ⟶ 4,423:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 4,297 ⟶ 4,595:
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="java">import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Line 4,444 ⟶ 4,742:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 4,608 ⟶ 4,906:
B • B • ◦ • ◦
• ◦ • ◦ W ◦ • </pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
 
'''Works with jq, the C implementation of jq'''
 
'''Works with gojq, the Go implementation of jq'''
 
In the following, positions on the chessboard are represented by {x,y} objects.
<syntaxhighlight lang="jq">
# Is the queen at position . attacking the position $pos ?
def isAttacking($pos):
.x == $pos.x or
.y == $pos.y or
(((.x - $pos.x)|length) == ((.y - $pos.y)|length)); # i.e. abs
 
# Place $q black and $q white queens on an $n*$n board,
# where .blackQueens holds the positions of existing black Queens,
# and similarly for .whiteQueens.
# input: {blackQueens, whiteQueens}
# output: updated input on success, otherwise null.
def place($queens; $n):
def place($q):
if $q == 0 then .ok = true
else .placingBlack = true
| first(
foreach range(0; $n) as $i (.;
foreach range(0; $n) as $j (.;
{x:$i, y:$j} as $pos
| .placingBlack as $placingBlack
| if any( .blackQueens[], .whiteQueens[];
((.x == $pos.x) and (.y == $pos.y)))
then . # failure
elif .placingBlack
then if any( .whiteQueens[]; isAttacking($pos) )
then .
else .blackQueens += [$pos]
| .placingBlack = false
end
elif any( .blackQueens[]; isAttacking($pos) )
then .
else .whiteQueens += [$pos]
| place($q-1) as $place
| if $place then $place # success
else .blackQueens |= .[:-1]
| .whiteQueens |= .[:-1]
| .placingBlack = true
end
end
| if $i == $n-1 and $j == $n-1 then .ok = false end );
select(.ok) )
) // null
end;
{blackQueens: [], whiteQueens: [] } | place($queens);
 
# Input {blackQueens, whiteQueens}
def printBoard($n):
[range(0; $n) | 0] as $row
| .board = [range(0; $n) | $row]
| reduce .blackQueens[] as $queen (.; .board[$queen.x][$queen.y] = "B ")
| reduce .whiteQueens[] as $queen (.; .board[$queen.x][$queen.y] = "W ")
| foreach range(0; $n) as $i (.;
reduce range(0; $n) as $j (.row="";
.board[$i][$j] as $b
| .row +=
(if $b != 0 then $b
elif $i%2 == $j%2
then "• "
else "◦ "
end) ) )
| .row;
 
# Use an object {squares, queens} to record the task:
# $squares is the number of squares on each side of the board,
# and $queens is the number of queens of each color.
def Task($squares; $queens): {$squares, $queens};
 
def tasks: [
Task(2; 1), Task(3; 1), Task(3; 2), Task(4; 1), Task(4; 2), Task(4; 3),
Task(5; 1), Task(5; 2), Task(5; 3), Task(5; 4), Task(5; 5),
Task(6; 1), Task(6; 2), Task(6; 3), Task(6; 4), Task(6; 5), Task(6; 6),
Task(7; 1), Task(7; 2), Task(7; 3), Task(7; 4), Task(7; 5), Task(7; 6), Task(7; 7)
];
 
tasks[] as $t
| "\($t.queens) black and \($t.queens) white queens on a \($t.squares) x \($t.squares) board:",
((place($t.queens; $t.squares)
| select(.)
| printBoard($t.squares))
// "No solution exists."),
""
</syntaxhighlight>
{{output}}
See [[#Wren|Wren]].
 
=={{header|Julia}}==
GUI version, uses the Gtk library. The place! function is condensed from the C# example.
<langsyntaxhighlight lang="julia">using Gtk
 
struct Position
Line 4,716 ⟶ 5,108:
 
peacefulqueenapp()
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{trans|D}}
<langsyntaxhighlight lang="scala">import kotlin.math.abs
 
enum class Piece {
Line 4,822 ⟶ 5,214:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 4,988 ⟶ 5,380:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[ValidSpots, VisibleByQueen, SolveQueen, GetSolution]
VisualizeState[state_] := Module[{q, cells},
q = MapIndexed[If[#["q"] == -1, {}, Text[Style[#["q"], 24], #2]] &, state, {2}];
Line 5,031 ⟶ 5,423:
]
GetSolution[8, 4, 3](* Solves placing 3 armies of each 4 queens on an 8*8 board*)
GetSolution[5, 4, 2](* Solves placing 2 armies of each 4 queens on an 5*5 board*)</langsyntaxhighlight>
{{out}}
<pre>[Graphical object]
Line 5,054 ⟶ 5,446:
Almost a direct translation except for "printBoard" where we have chosen to use a sequence of sequences to simplify the code.
 
<langsyntaxhighlight Nimlang="nim">import sequtils, strformat
 
type
Line 5,127 ⟶ 5,519:
printBoard(n, blackQueens, whiteQueens)
else:
echo "No solution exists.\n"</langsyntaxhighlight>
 
{{out}}
Line 5,296 ⟶ 5,688:
=={{header|Perl}}==
===Terse===
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 5,314 ⟶ 5,706:
(my $have = tr/WB//) < $m * 2 or exit !print "Solution to $m $n\n\n$_";
place( s/-\G/ qw(W B)[$have % 2] /er ) while /-/g; # place next queen
}</langsyntaxhighlight>
{{out}}
<pre>Solution to 4 5
Line 5,325 ⟶ 5,717:
===Verbose===
A refactored version of the same code, with fancier output.
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 5,376 ⟶ 5,768:
say $solution
? sprintf "Solution to $m $n\n\n%s", map { s/(.)/$1 /gm; s/B /♛/gm; s/W /♕/gmr } $solution
: "No solution to $m $n";</langsyntaxhighlight>
{{out}}
<pre>Solution to 4 5
Line 5,389 ⟶ 5,781:
{{trans|Go}}
{{trans|Python}}
You can run this online [http://phix.x10.mx/p2js/QueenArmies.htm here].
<lang Phix>-- demo\rosetta\Queen_Armies.exw
<!--<syntaxhighlight lang="phix">(phixonline)-->
string html = ""
<span style="color: #000080;font-style:italic;">--
constant as_html = true
-- demo\rosetta\Queen_Armies.exw
constant queens = {``,
-- =============================
`&#x265b;`,
--</span>
`<font color="green">&#x2655;</font>`,
<span style="color: #008080;">with</span> `<span style="color:red #008080;">?javascript_semantics</span>`}
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.2"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (puts(fn,x,false) for p2js.js)</span>
 
<span style="color: #004080;">string</span> <span style="color: #000000;">html</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
procedure showboard(integer n, sequence blackqueens, whitequeens)
<span style="color: #008080;">constant</span> <span style="color: #000000;">as_html</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
sequence board = repeat(repeat('-',n),n)
<span style="color: #008080;">constant</span> <span style="color: #000000;">queens</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">``</span><span style="color: #0000FF;">,</span>
for i=1 to length(blackqueens) do
<span style="color: #008000;">`&#x265b;`</span><span style="color: #0000FF;">,</span>
integer {qi,qj} = blackqueens[i]
<span style="color: #008000;">`&lt;font color="green"&gt;&#x2655;&lt;/font&gt;`</span><span style="color: #0000FF;">,</span>
board[qi,qj] = 'B'
<span style="color: #008000;">`&lt;span style="color:red"&gt;?&lt;/span&gt;`</span><span style="color: #0000FF;">}</span>
{qi,qj} = whitequeens[i]
board[qi,qj] = 'W'
<span style="color: #008080;">procedure</span> <span style="color: #000000;">showboard</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">blackqueens</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">whitequeens</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #004080;">sequence</span> <span style="color: #000000;">board</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'-'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
if as_html then
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">blackqueens</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
string out = sprintf("<br><b>## %d black and %d white queens on a %d-by-%d board</b><br>\n",
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">qi</span><span style="color: #0000FF;">,</span><span style="color: #000000;">qj</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">blackqueens</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
{length(blackqueens),length(whitequeens),n,n}),
<span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">qi</span><span style="color: #0000FF;">,</span><span style="color: #000000;">qj</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'B'</span>
tbl = ""
<span style="color: #0000FF;">{</span><span style="color: #000000;">qi</span><span style="color: #0000FF;">,</span><span style="color: #000000;">qj</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">whitequeens</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
out &= "<table style=\"font-weight:bold\">\n "
<span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">qi</span><span style="color: #0000FF;">,</span><span style="color: #000000;">qj</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'W'</span>
for x=1 to n do
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
for y=1 to n do
<span style="color: #008080;">if</span> <span style="color: #000000;">as_html</span> <span style="color: #008080;">then</span>
if y=1 then tbl &= " </tr>\n <tr valign=\"middle\" align=\"center\">\n" end if
<span style="color: #004080;">string</span> <span style="color: #000000;">out</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"&lt;br&gt;&lt;b&gt;## %d black and %d white queens on a %d-by-%d board&lt;/b&gt;&lt;br&gt;\n"</span><span style="color: #0000FF;">,</span>
integer xw = find({x,y},blackqueens)!=0,
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">blackqueens</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">whitequeens</span><span style="color: #0000FF;">),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">}),</span>
xb = find({x,y},whitequeens)!=0,
<span style="color: #000000;">tbl</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
dx = xw+xb*2+1
<span style="color: #000000;">out</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">"&lt;table style=\"font-weight:bold\"&gt;\n "</span>
string ch = queens[dx],
<span style="color: #008080;">for</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
bg = iff(mod(x+y,2)?"":` bgcolor="silver"`)
<span style="color: #008080;">for</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
tbl &= sprintf(" <td style=\"width:14pt; height:14pt;\"%s>%s</td>\n",{bg,ch})
<span style="color: #008080;">if</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #000000;">tbl</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">" &lt;/tr&gt;\n &lt;tr valign=\"middle\" align=\"center\"&gt;\n"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #004080;">integer</span> <span style="color: #000000;">xw</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">({</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">},</span><span style="color: #000000;">blackqueens</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span>
end for
<span style="color: #000000;">xb</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">({</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">},</span><span style="color: #000000;">whitequeens</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span>
out &= tbl[11..$]
<span style="color: #000000;">dx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">xw</span><span style="color: #0000FF;">+</span><span style="color: #000000;">xb</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
out &= " </tr>\n</table>\n<br>\n"
<span style="color: #004080;">string</span> <span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">queens</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">],</span>
html &= out
<span style="color: #000000;">bg</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)?</span><span style="color: #008000;">""</span><span style="color: #0000FF;">:</span><span style="color: #008000;">` bgcolor="silver"`</span><span style="color: #0000FF;">)</span>
else
<span style="color: #000000;">tbl</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">" &lt;td style=\"width:14pt; height:14pt;\"%s&gt;%s&lt;/td&gt;\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">bg</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">})</span>
integer b = length(blackqueens),
<span style="color: #008080;">end</span> w<span style="color: length(whitequeens)#008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1,"%d black and %d white queens on a %d x %d board:\n", {b, w, n, n})
<span style="color: #000000;">out</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">tbl</span><span style="color: #0000FF;">[</span><span style="color: #000000;">11</span><span style="color: #0000FF;">..$]</span>
puts(1,join(board,"\n")&"\n")
<span style="color: #000000;">out</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">" &lt;/tr&gt;\n&lt;/table&gt;\n&lt;br&gt;\n"</span>
-- ?{n,blackqueens, whitequeens}
<span style="color: #000000;">html</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">out</span>
end if
<span style="color: #008080;">else</span>
end procedure
<span style="color: #004080;">integer</span> <span style="color: #000000;">b</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">blackqueens</span><span style="color: #0000FF;">),</span>
 
<span style="color: #000000;">w</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">whitequeens</span><span style="color: #0000FF;">)</span>
function isAttacking(sequence queen, pos)
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d black and %d white queens on a %d x %d board:\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">})</span>
integer {qi,qj} = queen, {pi,pj} = pos
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">board</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
return qi=pi or qj=pj or abs(qi-pi)=abs(qj-pj)
<span style="color: #000080;font-style:italic;">-- ?{n,blackqueens, whitequeens}</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
function place(integer m, n, sequence blackqueens = {}, whitequeens = {})
if m == 0 then showboard(n,blackqueens,whitequeens) return true end if
<span style="color: #008080;">function</span> <span style="color: #000000;">isAttacking</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">queen</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pos</span><span style="color: #0000FF;">)</span>
bool placingBlack := true
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">qi</span><span style="color: #0000FF;">,</span><span style="color: #000000;">qj</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">queen</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pj</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pos</span>
for i=1 to n do
<span style="color: #008080;">return</span> <span style="color: #000000;">qi</span><span style="color: #0000FF;">=</span><span style="color: #000000;">pi</span> <span style="color: #008080;">or</span> <span style="color: #000000;">qj</span><span style="color: #0000FF;">=</span><span style="color: #000000;">pj</span> <span style="color: #008080;">or</span> <span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">qi</span><span style="color: #0000FF;">-</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">)=</span><span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">qj</span><span style="color: #0000FF;">-</span><span style="color: #000000;">pj</span><span style="color: #0000FF;">)</span>
for j=1 to n do
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
sequence pos := {i, j}
for q=1 to length(blackqueens) do
<span style="color: #008080;">function</span> <span style="color: #000000;">place</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">blackqueens</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000000;">whitequeens</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{})</span>
sequence queen := blackqueens[q]
<span style="color: #008080;">if</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">==</span> <span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">showboard</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">blackqueens</span><span style="color: #0000FF;">,</span><span style="color: #000000;">whitequeens</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #004600;">true</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if queen == pos or ((not placingBlack) and isAttacking(queen, pos)) then
<span style="color: #004080;">bool</span> <span style="color: #000000;">placingBlack</span> <span style="color: #0000FF;">:=</span> <span style="color: #004600;">true</span>
pos = {}
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
exit
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
end if
<span style="color: #004080;">sequence</span> <span style="color: #000000;">pos</span> <span style="color: #0000FF;">:=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">}</span>
end for
<span style="color: #008080;">for</span> <span style="color: #000000;">q</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">blackqueens</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
if pos!={} then
<span style="color: #004080;">sequence</span> <span style="color: #000000;">queen</span> <span style="color: #0000FF;">:=</span> <span style="color: #000000;">blackqueens</span><span style="color: #0000FF;">[</span><span style="color: #000000;">q</span><span style="color: #0000FF;">]</span>
for q=1 to length(whitequeens) do
<span style="color: #008080;">if</span> <span style="color: #000000;">queen</span> <span style="color: #0000FF;">==</span> <span style="color: #000000;">pos</span> <span style="color: #008080;">or</span> <span style="color: #0000FF;">((</span><span style="color: #008080;">not</span> <span style="color: #000000;">placingBlack</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #000000;">isAttacking</span><span style="color: #0000FF;">(</span><span style="color: #000000;">queen</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pos</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">then</span>
sequence queen := whitequeens[q]
if queen<span style=="color: #000000;">pos</span> or<span (placingBlackstyle="color: and#0000FF;">=</span> isAttacking(queen,<span pos))style="color: then#0000FF;">{}</span>
<span pos style="color: {}#008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: exit#008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">pos</span><span style="color: #0000FF;">!={}</span> <span style="color: #008080;">then</span>
end for
<span style="color: #008080;">for</span> <span style="color: #000000;">q</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">whitequeens</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
if pos!={} then
<span style="color: #004080;">sequence</span> <span style="color: #000000;">queen</span> <span style="color: #0000FF;">:=</span> <span style="color: #000000;">whitequeens</span><span style="color: #0000FF;">[</span><span style="color: #000000;">q</span><span style="color: #0000FF;">]</span>
if placingBlack then
<span style="color: #008080;">if</span> <span style="color: #000000;">queen</span> <span style="color: #0000FF;">==</span> <span style="color: #000000;">pos</span> <span style="color: #008080;">or</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">placingBlack</span> <span style="color: #008080;">and</span> <span style="color: #000000;">isAttacking</span><span style="color: #0000FF;">(</span><span style="color: #000000;">queen</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pos</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">then</span>
blackqueens = append(blackqueens, pos)
<span style="color: #000000;">pos</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
placingBlack = false
else <span style="color: #008080;">exit</span>
<span style="color: #008080;">end</span> whitequeens<span style="color: append(whitequeens, pos)#008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
if place(m-1, n, blackqueens, whitequeens) then return true end if
<span style="color: #008080;">if</span> <span style="color: #000000;">pos</span><span style="color: #0000FF;">!={}</span> <span style="color: #008080;">then</span>
blackqueens = blackqueens[1..$-1]
<span style="color: #008080;">if</span> <span style="color: #000000;">placingBlack</span> <span style="color: #008080;">then</span>
whitequeens = whitequeens[1..$-1]
<span style="color: #000000;">blackqueens</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">blackqueens</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">pos</span><span style="color: #0000FF;">)</span>
placingBlack = true
<span style="color: #000000;">placingBlack</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
end if
end if <span style="color: #008080;">else</span>
<span style="color: #000000;">whitequeens</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">whitequeens</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">pos</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">place</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">blackqueens</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">whitequeens</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">true</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #000000;">blackqueens</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">blackqueens</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
end for
<span style="color: #000000;">whitequeens</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">whitequeens</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
return false
<span style="color: #000000;">placingBlack</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
for n=2 to 7 do
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
for m=1 to n-(n<5) do
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
if not place(m,n) then
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
string no = sprintf("Cannot place %d+ queen armies on a %d-by-%d board",{m,n,n})
<span style="color: #008080;">return</span> <span style="color: #004600;">false</span>
if as_html then
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
html &= sprintf("<b># %s</b><br><br>\n\n",{no})
else
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">7</span> <span style="color: #008080;">do</span>
printf(1,"%s.\n", {no})
<span style="color: #008080;">for</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">-(</span><span style="color: #000000;">n</span><span style="color: #0000FF;"><</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
end if
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">place</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
end if
<span style="color: #004080;">string</span> <span style="color: #000000;">no</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Cannot place %d+ queen armies on a %d-by-%d board"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">})</span>
end for
<span style="color: #008080;">if</span> <span style="color: #000000;">as_html</span> <span style="color: #008080;">then</span>
end for
<span style="color: #000000;">html</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"&lt;b&gt;# %s&lt;/b&gt;&lt;br&gt;&lt;br&gt;\n\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">no</span><span style="color: #0000FF;">})</span>
 
<span style="color: #008080;">else</span>
constant html_header = """
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s.\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">no</span><span style="color: #0000FF;">})</span>
<!DOCTYPE html>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<html lang="en">
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<head>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<meta charset="utf-8" />
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Rosettacode Rank Languages by popularity</title>
<span style="color: #008080;">constant</span> <span style="color: #000000;">html_header</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
</head>
&lt;!DOCTYPE html&gt;
<body>
&lt;html lang="en"&gt;
<h2>queen armies</h2>
&lt;head&gt;
""", -- or <div style="overflow:scroll; height:250px;">
&lt;meta charset="utf-8" /&gt;
html_footer = """
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
</body>
&lt;title&gt;Queen Armies&lt;/title&gt;
</html>
&lt;/head&gt;
""" -- or </div>
&lt;body&gt;
 
&lt;h2&gt;queen armies&lt;/h2&gt;
if as_html then
"""</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- or &lt;div style="overflow:scroll; height:250px;"&gt;</span>
integer fn = open("queen_armies.html","w")
<span style="color: #000000;">html_footer</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
puts(fn,html_header)
&lt;/body&gt;
puts(fn,html)
&lt;/html&gt;
puts(fn,html_footer)
"""</span> <span style="color: #000080;font-style:italic;">-- or &lt;/div&gt;</span>
close(fn)
printf(1,"See queen_armies.html\n")
<span style="color: #008080;">if</span> <span style="color: #000000;">as_html</span> <span style="color: #008080;">then</span>
end if
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
 
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">html</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
?"done"
<span style="color: #008080;">else</span>
{} = wait_key()</lang>
<span style="color: #004080;">integer</span> <span style="color: #000000;">fn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">open</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"queen_armies.html"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"w"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">html_header</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">html</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">html_footer</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"See queen_armies.html\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #0000FF;">?</span><span style="color: #008000;">"done"</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
{{out}}
with as_html = false
Line 5,634 ⟶ 6,038:
=={{header|Python}}==
===Python: Textual output===
<langsyntaxhighlight lang="python">from itertools import combinations, product, count
from functools import lru_cache, reduce
 
Line 5,707 ⟶ 6,111:
m, n = 5, 7
ans = place(m, n)
pboard(ans, n)</langsyntaxhighlight>
 
{{out}}
Line 5,807 ⟶ 6,211:
===Python: HTML output===
Uses the solver function <code>place</code> from the above textual output case.
<langsyntaxhighlight lang="python">from peaceful_queen_armies_simpler import place
from itertools import product, count
 
Line 5,855 ⟶ 6,259:
html += hboard(ans, n)
with open('peaceful_queen_armies.htm', 'w') as f:
f.write(html)</langsyntaxhighlight>
 
{{out}}
Line 6,449 ⟶ 6,853:
(formerly Perl 6)
{{trans|Perl}}
<syntaxhighlight lang="raku" perl6line># recursively place the next queen
sub place ($board, $n, $m, $empty-square) {
my $cnt;
Line 6,496 ⟶ 6,900:
say $solution
?? "Solution to $m $n\n\n{S:g/(\N)/$0 / with $solution}"
!! "No solution to $m $n";</langsyntaxhighlight>
{{out}}
<pre>W • ◦ • W
Line 6,506 ⟶ 6,910:
=={{header|Ruby}}==
{{trans|Java}}
<langsyntaxhighlight lang="ruby">class Position
attr_reader :x, :y
 
Line 6,625 ⟶ 7,029:
print "No solution exists.\n\n"
end
end</langsyntaxhighlight>
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 6,795 ⟶ 7,199:
{{libheader|srfi-132}}
 
<langsyntaxhighlight lang="scheme">;;;
;;; Solutions to the Peaceful Chess Queen Armies puzzle, in R7RS
;;; Scheme (using also SRFI-132).
Line 7,024 ⟶ 7,428:
(newline)
(newline)
(loop (+ next-solution-number 1))))))</langsyntaxhighlight>
 
{{out}}
Line 7,265 ⟶ 7,669:
===All non-equivalent solutions===
{{works with|CHICKEN|5.3.0}}
<langsyntaxhighlight lang="scheme">;;;
;;; Solutions to the Peaceful Chess Queen Armies puzzle, in R7RS
;;; Scheme. This implementation returns only one of each equivalent
Line 7,606 ⟶ 8,010:
(newline)
(newline)
(loop (+ next-solution-number 1))))))</langsyntaxhighlight>
 
{{out}}
Line 7,654 ⟶ 8,058:
{{trans|Kotlin}}
 
<langsyntaxhighlight lang="swift">enum Piece {
case empty, black, white
}
Line 7,762 ⟶ 8,166:
print("No solution")
}
}</langsyntaxhighlight>
 
{{out}}
Line 7,927 ⟶ 8,331:
{{trans|Kotlin}}
{{libheader|Wren-dynamic}}
<langsyntaxhighlight ecmascriptlang="wren">import "./dynamic" for Enum, Tuple
 
var Piece = Enum.create("Piece", ["empty", "black", "white"])
Line 8,019 ⟶ 8,423:
System.print("No solution exists.\n")
}
}</langsyntaxhighlight>
 
{{out}}
Line 8,027 ⟶ 8,431:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn isAttacked(q, x,y) // ( (r,c), x,y ) : is queen at r,c attacked by q@(x,y)?
{ r,c:=q; (r==x or c==y or r+c==x+y or r-c==x-y) }
fcn isSafe(r,c,qs) // queen safe at (r,c)?, qs=( (r,c),(r,c)..)
Line 8,059 ⟶ 8,463:
z.text.pump(Void,T(Void.Read,N-1),"println");
}
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">peacefulQueens();
foreach n in ([4..10]){ peacefulQueens(n,n) }</langsyntaxhighlight>
{{out}}
<pre>
2,442

edits