Peaceful chess queen armies: Difference between revisions
m
→{{header|jq}}: add newline
m (→{{header|jq}}: add newline) |
|||
(19 intermediate revisions by 6 users not shown) | |||
Line 58:
{{trans|D}}
<
EMPTY
BLACK
Line 138:
printBoard(nm[0], blackQueens, whiteQueens)
E
print("No solution exists.\n")</
{{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 can solve the m=5, n=6 case in practical time on a fast machine. m=7, n=7 is a more annoying case.)
<syntaxhighlight lang="ats">(********************************************************************)
#define ATS_DYNLOADFLAG 0
Line 1,258 ⟶ 1,260:
end
(********************************************************************)</
{{out}}
Line 1,304 ⟶ 1,306:
=={{header|C}}==
{{trans|C#}}
<
#include <stdbool.h>
#include <stdio.h>
Line 1,576 ⟶ 1,578:
return EXIT_SUCCESS;
}</
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 1,743 ⟶ 1,745:
=={{header|C sharp|C#}}==
{{trans|D}}
<
using System.Collections.Generic;
Line 1,856 ⟶ 1,858:
}
}
}</
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 2,023 ⟶ 2,025:
=={{header|C++}}==
{{trans|D}}
<
#include <vector>
Line 2,137 ⟶ 2,139:
return 0;
}</
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 2,304 ⟶ 2,306:
=={{header|D}}==
{{trans|Go}}
<
import std.math;
import std.stdio;
Line 2,415 ⟶ 2,417:
}
}
}</
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 2,579 ⟶ 2,581:
◦ • ◦ W ◦ • ◦
W ◦ W W • ◦ • </pre>
=={{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.
For speed, armies are represented by 64-bit or 128-bit integers, depending on the value of n. A 1-bit represets a queen. Rotations and reflections of the board are elemental integer operations on an army. Checking for any attacks is an elemental integer-to-boolean operation on the two armies (though the program detects rook-like attacks by a different mechanism). Equivalence under interchange of the colors can be tested by reversing which army gets which integer value.
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:
<syntaxhighlight lang="fortran">program peaceful_queens_elements_generator
use, intrinsic :: iso_fortran_env, only: int64
use, intrinsic :: iso_fortran_env, only: error_unit
implicit none
! 64-bit integers, for boards up to 8-by-8.
integer, parameter :: kind8x8 = int64
! 128-bit integers, for boards up to 11-by-11.
! This value is correct for gfortran.
integer, parameter :: kind11x11 = 16
integer(kind = kind11x11), parameter :: one = 1
integer(kind = kind11x11), parameter :: two = 2
integer, parameter :: n_max = 11
integer(kind = kind11x11) :: rook1_masks(0 : n_max - 1)
integer(kind = kind11x11) :: rook2_masks(0 : n_max - 1)
integer(kind = kind11x11) :: bishop1_masks(0 : (2 * n_max) - 4)
integer(kind = kind11x11) :: bishop2_masks(0 : (2 * n_max) - 4)
! Combines rook1_masks and rook2_masks.
integer(kind = kind11x11) :: rook_masks(0 : (2 * n_max) - 1)
! Combines bishop1_masks and bishop2_masks.
integer(kind = kind11x11) :: bishop_masks(0 : (4 * n_max) - 7)
! Combines rook and bishop masks.
integer(kind = kind11x11) :: queen_masks(0 : (6 * n_max) - 7)
character(len = 16), parameter :: s_kind8x8 = "kind8x8 "
character(len = 16), parameter :: s_kind11x11 = "kind11x11 "
character(200) :: arg
integer :: arg_count
integer :: m, n, max_solutions
integer :: board_kind
arg_count = command_argument_count ()
if (arg_count /= 3) then
call get_command_argument (0, arg)
write (error_unit, '("Usage: ", A, " M N MAX_SOLUTIONS")') trim (arg)
stop 1
end if
call get_command_argument (1, arg)
read (arg, *) m
if (m < 1) then
write (error_unit, '("M must be between 1 or greater.")')
stop 2
end if
call get_command_argument (2, arg)
read (arg, *) n
if (n < 3 .or. 11 < n) then
write (error_unit, '("N must be between 3 and ", I0, ", inclusive.")') n_max
stop 2
end if
call get_command_argument (3, arg)
read (arg, *) max_solutions
write (*, '("module peaceful_queens_elements")')
write (*, '()')
write (*, '(" use, intrinsic :: iso_fortran_env, only: int64")')
write (*, '()')
write (*, '(" implicit none")')
write (*, '(" private")')
write (*, '()')
write (*, '(" integer, parameter, public :: m = ", I0)') m
write (*, '(" integer, parameter, public :: n = ", I0)') n
write (*, '(" integer, parameter, public :: max_solutions = ", I0)') max_solutions
write (*, '()')
if (n <= 8) then
write (*, '(" ! 64-bit integers, for boards up to 8-by-8.")')
write (*, '(" integer, parameter, private :: kind8x8 = int64")')
else
write (*, '(" ! 128-bit integers, for boards up to 11-by-11.")')
write (*, '(" integer, parameter, private :: kind11x11 = ", I0)') kind11x11
end if
write (*, '(" integer, parameter, public :: board_kind = ", A)') trim (s_kindnxn (n))
write (*, '()')
write (*, '()')
write (*, '(" public :: rooks1_attack_check")')
write (*, '(" public :: rooks2_attack_check")')
write (*, '(" public :: rooks_attack_check")')
write (*, '(" public :: bishops1_attack_check")')
write (*, '(" public :: bishops2_attack_check")')
write (*, '(" public :: bishops_attack_check")')
write (*, '(" public :: queens_attack_check")')
write (*, '()')
write (*, '(" public :: board_rotate90")')
write (*, '(" public :: board_rotate180")')
write (*, '(" public :: board_rotate270")')
write (*, '(" public :: board_reflect1")')
write (*, '(" public :: board_reflect2")')
write (*, '(" public :: board_reflect3")')
write (*, '(" public :: board_reflect4")')
write (*, '()')
call write_rook1_masks
call write_rook2_masks
call write_bishop1_masks
call write_bishop2_masks
call write_rook_masks
call write_bishop_masks
call write_queen_masks
write (*, '("contains")')
write (*, '()')
call write_rooks1_attack_check
call write_rooks2_attack_check
call write_bishops1_attack_check
call write_bishops2_attack_check
call write_rooks_attack_check
call write_bishops_attack_check
call write_queens_attack_check
call write_board_rotate90
call write_board_rotate180
call write_board_rotate270
call write_board_reflect1
call write_board_reflect2
call write_board_reflect3
call write_board_reflect4
call write_insert_zeros
call write_reverse_insert_zeros
write (*, '("end module peaceful_queens_elements")')
contains
subroutine write_rook1_masks
integer :: i
call fill_masks (n)
do i = 0, n - 1
write (*, '(" integer(kind = ", A, "), parameter :: rook1_mask_",&
& I0, "x", I0, "_", I0, " = int (z''", Z0.32, "'', kind &
&= ", A, ")")') trim (s_kindnxn (n)), n, n, i,&
& rook1_masks(i), trim (s_kindnxn (n))
end do
write (*, '()')
end subroutine write_rook1_masks
subroutine write_rook2_masks
integer :: i
call fill_masks (n)
do i = 0, n - 1
write (*, '(" integer(kind = ", A, "), parameter :: rook2_mask_",&
& I0, "x", I0, "_", I0, " = int (z''", Z0.32, "'', kind &
&= ", A, ")")') trim (s_kindnxn (n)), n, n, i,&
& rook2_masks(i), trim (s_kindnxn (n))
end do
write (*, '()')
end subroutine write_rook2_masks
subroutine write_bishop1_masks
integer :: i
call fill_masks (n)
do i = 0, (2 * n) - 4
write (*, '(" integer(kind = ", A, "), parameter :: bishop1_mask_",&
& I0, "x", I0, "_", I0, " = int (z''", Z0.32, "'', kind &
&= ", A, ")")') trim (s_kindnxn (n)), n, n, i,&
& bishop1_masks(i), trim (s_kindnxn (n))
end do
write (*, '()')
end subroutine write_bishop1_masks
subroutine write_bishop2_masks
integer :: i
call fill_masks (n)
do i = 0, (2 * n) - 4
write (*, '(" integer(kind = ", A, "), parameter :: bishop2_mask_",&
& I0, "x", I0, "_", I0, " = int (z''", Z0.32, "'', kind &
&= ", A, ")")') trim (s_kindnxn (n)), n, n, i,&
& bishop2_masks(i), trim (s_kindnxn (n))
end do
write (*, '()')
end subroutine write_bishop2_masks
subroutine write_rook_masks
integer :: i
call fill_masks (n)
do i = 0, (2 * n) - 1
write (*, '(" integer(kind = ", A, "), parameter :: rook_mask_",&
& I0, "x", I0, "_", I0, " = int (z''", Z0.32, "'', kind &
&= ", A, ")")') trim (s_kindnxn (n)), n, n, i,&
& rook_masks(i), trim (s_kindnxn (n))
end do
write (*, '()')
end subroutine write_rook_masks
subroutine write_bishop_masks
integer :: i
call fill_masks (n)
do i = 0, (4 * n) - 7
write (*, '(" integer(kind = ", A, "), parameter :: bishop_mask_",&
& I0, "x", I0, "_", I0, " = int (z''", Z0.32, "'', kind &
&= ", A, ")")') trim (s_kindnxn (n)), n, n, i,&
& bishop_masks(i), trim (s_kindnxn (n))
end do
write (*, '()')
end subroutine write_bishop_masks
subroutine write_queen_masks
integer :: i
call fill_masks (n)
do i = 0, (6 * n) - 7
write (*, '(" integer(kind = ", A, "), parameter :: queen_mask_",&
& I0, "x", I0, "_", I0, " = int (z''", Z0.32, "'', kind &
&= ", A, ")")') trim (s_kindnxn (n)), n, n, i,&
& queen_masks(i), trim (s_kindnxn (n))
end do
write (*, '()')
end subroutine write_queen_masks
subroutine write_rooks1_attack_check
integer :: i
write (*, '(" elemental function rooks1_attack_check (army1, army2) result (attacking)")')
write (*, '(" integer(kind = ", A, "), value :: army1, army2")') trim (s_kindnxn (n))
write (*, '(" logical :: attacking")')
write (*, '()')
write (*, '(" attacking = ((iand (army1, rook1_mask_", I0, "x", I0,&
& "_0) /= 0) .and. (iand (army2, rook1_mask_", I0, "x", I0, "_0) /=&
& 0)) .or. &")') n, n, n, n
do i = 1, n - 1
write (*, '(" & ((iand (army1, rook1_mask_", I0, "x",&
& I0, "_", I0, ") /= 0) .and. (iand (army2, rook1_mask_", I0,&
& "x", I0, "_", I0, ") /= 0))")', advance = 'no') n, n, i, n, n, i
if (i /= n - 1) then
write (*, '(" .or. &")')
else
write (*, '()')
end if
end do
write (*, '(" end function rooks1_attack_check")')
write (*, '()')
end subroutine write_rooks1_attack_check
subroutine write_rooks2_attack_check
integer :: i
write (*, '(" elemental function rooks2_attack_check (army1, army2) result (attacking)")')
write (*, '(" integer(kind = ", A, "), value :: army1, army2")') trim (s_kindnxn (n))
write (*, '(" logical :: attacking")')
write (*, '()')
write (*, '(" attacking = ((iand (army1, rook2_mask_", I0, "x", I0,&
& "_0) /= 0) .and. (iand (army2, rook2_mask_", I0, "x", I0, "_0) /=&
& 0)) .or. &")') n, n, n, n
do i = 1, n - 1
write (*, '(" & ((iand (army1, rook2_mask_", I0, "x",&
& I0, "_", I0, ") /= 0) .and. (iand (army2, rook2_mask_", I0,&
& "x", I0, "_", I0, ") /= 0))")', advance = 'no') n, n, i, n, n, i
if (i /= n - 1) then
write (*, '(" .or. &")')
else
write (*, '()')
end if
end do
write (*, '(" end function rooks2_attack_check")')
write (*, '()')
end subroutine write_rooks2_attack_check
subroutine write_bishops1_attack_check
integer :: i
write (*, '(" elemental function bishops1_attack_check (army1, army2) result (attacking)")')
write (*, '(" integer(kind = ", A, "), value :: army1, army2")') trim (s_kindnxn (n))
write (*, '(" logical :: attacking")')
write (*, '()')
write (*, '(" attacking = ((iand (army1, bishop1_mask_", I0, "x", I0,&
& "_0) /= 0) .and. (iand (army2, bishop1_mask_", I0, "x", I0, "_0) /=&
& 0)) .or. &")') n, n, n, n
do i = 1, (2 * n) - 4
write (*, '(" & ((iand (army1, bishop1_mask_", I0, "x",&
& I0, "_", I0, ") /= 0) .and. (iand (army2, bishop1_mask_", I0,&
& "x", I0, "_", I0, ") /= 0))")', advance = 'no') n, n, i, n, n, i
if (i /= (2 * n) - 4) then
write (*, '(" .or. &")')
else
write (*, '()')
end if
end do
write (*, '(" end function bishops1_attack_check")')
write (*, '()')
end subroutine write_bishops1_attack_check
subroutine write_bishops2_attack_check
integer :: i
write (*, '(" elemental function bishops2_attack_check (army1, army2) result (attacking)")')
write (*, '(" integer(kind = ", A, "), value :: army1, army2")') trim (s_kindnxn (n))
write (*, '(" logical :: attacking")')
write (*, '()')
write (*, '(" attacking = ((iand (army1, bishop2_mask_", I0, "x", I0,&
& "_0) /= 0) .and. (iand (army2, bishop2_mask_", I0, "x", I0, "_0) /=&
& 0)) .or. &")') n, n, n, n
do i = 1, (2 * n) - 4
write (*, '(" & ((iand (army1, bishop2_mask_", I0, "x",&
& I0, "_", I0, ") /= 0) .and. (iand (army2, bishop2_mask_", I0,&
& "x", I0, "_", I0, ") /= 0))")', advance = 'no') n, n, i, n, n, i
if (i /= (2 * n) - 4) then
write (*, '(" .or. &")')
else
write (*, '()')
end if
end do
write (*, '(" end function bishops2_attack_check")')
write (*, '()')
end subroutine write_bishops2_attack_check
subroutine write_rooks_attack_check
integer :: i
write (*, '(" elemental function rooks_attack_check (army1, army2) result (attacking)")')
write (*, '(" integer(kind = ", A, "), value :: army1, army2")') trim (s_kindnxn (n))
write (*, '(" logical :: attacking")')
write (*, '()')
write (*, '(" attacking = ((iand (army1, rook_mask_", I0, "x", I0,&
& "_0) /= 0) .and. (iand (army2, rook_mask_", I0, "x", I0, "_0) /=&
& 0)) .or. &")') n, n, n, n
do i = 1, (2 * n) - 1
write (*, '(" & ((iand (army1, rook_mask_", I0, "x",&
& I0, "_", I0, ") /= 0) .and. (iand (army2, rook_mask_", I0,&
& "x", I0, "_", I0, ") /= 0))")', advance = 'no') n, n, i, n, n, i
if (i /= (2 * n) - 1) then
write (*, '(" .or. &")')
else
write (*, '()')
end if
end do
write (*, '(" end function rooks_attack_check")')
write (*, '()')
end subroutine write_rooks_attack_check
subroutine write_bishops_attack_check
integer :: i
write (*, '(" elemental function bishops_attack_check (army1, army2) result (attacking)")')
write (*, '(" integer(kind = ", A, "), value :: army1, army2")') trim (s_kindnxn (n))
write (*, '(" logical :: attacking")')
write (*, '()')
write (*, '(" attacking = ((iand (army1, bishop_mask_", I0, "x", I0,&
& "_0) /= 0) .and. (iand (army2, bishop_mask_", I0, "x", I0, "_0) /=&
& 0)) .or. &")') n, n, n, n
do i = 1, (4 * n) - 7
write (*, '(" & ((iand (army1, bishop_mask_", I0, "x",&
& I0, "_", I0, ") /= 0) .and. (iand (army2, bishop_mask_", I0,&
& "x", I0, "_", I0, ") /= 0))")', advance = 'no') n, n, i, n, n, i
if (i /= (4 * n) - 7) then
write (*, '(" .or. &")')
else
write (*, '()')
end if
end do
write (*, '(" end function bishops_attack_check")')
write (*, '()')
end subroutine write_bishops_attack_check
subroutine write_queens_attack_check
integer :: i
write (*, '(" elemental function queens_attack_check (army1, army2) result (attacking)")')
write (*, '(" integer(kind = ", A, "), value :: army1, army2")') trim (s_kindnxn (n))
write (*, '(" logical :: attacking")')
write (*, '()')
write (*, '(" attacking = ((iand (army1, queen_mask_", I0, "x", I0,&
& "_0) /= 0) .and. (iand (army2, queen_mask_", I0, "x", I0, "_0) /=&
& 0)) .or. &")') n, n, n, n
do i = 1, (6 * n) - 7
write (*, '(" & ((iand (army1, queen_mask_", I0, "x",&
& I0, "_", I0, ") /= 0) .and. (iand (army2, queen_mask_", I0,&
& "x", I0, "_", I0, ") /= 0))")', advance = 'no') n, n, i, n, n, i
if (i /= (6 * n) - 7) then
write (*, '(" .or. &")')
else
write (*, '()')
end if
end do
write (*, '(" end function queens_attack_check")')
write (*, '()')
end subroutine write_queens_attack_check
subroutine write_board_rotate90
integer :: i, j
write (*, '(" elemental function board_rotate90 (a) result (b)")')
write (*, '(" integer(kind = ", A, "), value :: a")') trim (s_kindnxn (n))
write (*, '(" integer(kind = ", A, ") :: b")') trim (s_kindnxn (n))
write (*, '()')
write (*, '(" ! Rotation 90 degrees in one of the orientations.")')
write (*, '()')
do i = 0, n - 1
if (i == 0) then
write (*, '(" b = ")', advance = 'no')
else
write (*, '(" & ")', advance = 'no')
do j = 1, i
write (*, '(" ")', advance = 'no')
end do
end if
if (i /= n - 1) then
write (*, '("ior (ishft (reverse_insert_zeros_", I0, " (ishft&
& (iand (rook1_mask_", I0, "x", I0, "_", I0, ", a), ",&
& I0, ")), ", I0, "), &")') n, n, n, i, -i * n, i
else
write (*, '(" ishft (reverse_insert_zeros_", I0, " (ishft&
& (iand (rook1_mask_", I0, "x", I0, "_", I0, ", a), ",&
& I0, ")), ", I0, ")")', advance = 'no') n, n, n, i, -i * n, i
do j = 1, n - 1
write (*, '(")")', advance = 'no')
end do
write (*, '()')
end if
end do
write (*, '(" end function board_rotate90")')
write (*, '()')
end subroutine write_board_rotate90
subroutine write_board_rotate180
write (*, '(" elemental function board_rotate180 (a) result (b)")')
write (*, '(" integer(kind = ", A, "), value :: a")') trim (s_kindnxn (n))
write (*, '(" integer(kind = ", A, ") :: b")') trim (s_kindnxn (n))
write (*, '()')
write (*, '(" ! Rotation 180 degrees.")')
write (*, '()')
write (*, '(" b = board_reflect1 (board_reflect2 (a))")')
write (*, '(" end function board_rotate180")')
write (*, '()')
end subroutine write_board_rotate180
subroutine write_board_rotate270
integer :: i, j
write (*, '(" elemental function board_rotate270 (a) result (b)")')
write (*, '(" integer(kind = ", A, "), value :: a")') trim (s_kindnxn (n))
write (*, '(" integer(kind = ", A, ") :: b")') trim (s_kindnxn (n))
write (*, '()')
write (*, '(" ! Rotation 270 degrees in one of the orientations.")')
write (*, '()')
do i = 0, n - 1
if (i == 0) then
write (*, '(" b = ")', advance = 'no')
else
write (*, '(" & ")', advance = 'no')
do j = 1, i
write (*, '(" ")', advance = 'no')
end do
end if
if (i /= n - 1) then
write (*, '("ior (ishft (insert_zeros_", I0, " (ishft&
& (iand (rook1_mask_", I0, "x", I0, "_", I0, ", a), ",&
& I0, ")), ", I0, "), &")') n, n, n, i, -i * n, n - 1 - i
else
write (*, '(" ishft (insert_zeros_", I0, " (ishft&
& (iand (rook1_mask_", I0, "x", I0, "_", I0, ", a), ",&
& I0, ")), ", I0, ")")', advance = 'no') n, n, n, i, -i * n, n - 1 - i
do j = 1, n - 1
write (*, '(")")', advance = 'no')
end do
write (*, '()')
end if
end do
write (*, '(" end function board_rotate270")')
write (*, '()')
end subroutine write_board_rotate270
subroutine write_board_reflect1
integer :: i, j
write (*, '(" elemental function board_reflect1 (a) result (b)")')
write (*, '(" integer(kind = ", A, "), value :: a")') trim (s_kindnxn (n))
write (*, '(" integer(kind = ", A, ") :: b")') trim (s_kindnxn (n))
write (*, '()')
write (*, '(" ! Reflection of rows or columns.")')
write (*, '()')
do i = 0, n - 1
if (i == 0) then
write (*, '(" b = ")', advance = 'no')
else
write (*, '(" & ")', advance = 'no')
do j = 1, i
write (*, '(" ")', advance = 'no')
end do
end if
if (i /= n - 1) then
write (*, '("ior (ishft (iand (rook2_mask_", I0, "x", I0, "_", I0, ", a), ", I0, "), &")') &
& n, n, i, (n - 1) - (2 * i)
else
write (*, '("ishft (iand (rook2_mask_", I0, "x", I0, "_", I0, ", a), ", I0, ")")', advance = 'no') &
& n, n, i, (n - 1) - (2 * i)
do j = 1, n - 1
write (*, '(")")', advance = 'no')
end do
write (*, '()')
end if
end do
write (*, '(" end function board_reflect1")')
write (*, '()')
end subroutine write_board_reflect1
subroutine write_board_reflect2
integer :: i, j
write (*, '(" elemental function board_reflect2 (a) result (b)")')
write (*, '(" integer(kind = ", A, "), value :: a")') trim (s_kindnxn (n))
write (*, '(" integer(kind = ", A, ") :: b")') trim (s_kindnxn (n))
write (*, '()')
write (*, '(" ! Reflection of rows or columns.")')
write (*, '()')
do i = 0, n - 1
if (i == 0) then
write (*, '(" b = ")', advance = 'no')
else
write (*, '(" & ")', advance = 'no')
do j = 1, i
write (*, '(" ")', advance = 'no')
end do
end if
if (i /= n - 1) then
write (*, '("ior (ishft (iand (rook1_mask_", I0, "x", I0, "_", I0, ", a), ", I0, "), &")') &
& n, n, i, n * ((n - 1) - (2 * i))
else
write (*, '("ishft (iand (rook1_mask_", I0, "x", I0, "_", I0, ", a), ", I0, ")")', advance = 'no') &
& n, n, i, n * ((n - 1) - (2 * i))
do j = 1, n - 1
write (*, '(")")', advance = 'no')
end do
write (*, '()')
end if
end do
write (*, '(" end function board_reflect2")')
write (*, '()')
end subroutine write_board_reflect2
subroutine write_board_reflect3
integer :: i, j
write (*, '(" elemental function board_reflect3 (a) result (b)")')
write (*, '(" integer(kind = ", A, "), value :: a")') trim (s_kindnxn (n))
write (*, '(" integer(kind = ", A, ") :: b")') trim (s_kindnxn (n))
write (*, '()')
write (*, '(" ! Reflection around one of the two main diagonals.")')
write (*, '()')
do i = 0, n - 1
if (i == 0) then
write (*, '(" b = ")', advance = 'no')
else
write (*, '(" & ")', advance = 'no')
do j = 1, i
write (*, '(" ")', advance = 'no')
end do
end if
if (i /= n - 1) then
write (*, '("ior (ishft (insert_zeros_", I0, " (ishft&
& (iand (rook1_mask_", I0, "x", I0, "_", I0, ", a), ",&
& I0, ")), ", I0, "), &")') n, n, n, i, -i * n, i
else
write (*, '(" ishft (insert_zeros_", I0, " (ishft&
& (iand (rook1_mask_", I0, "x", I0, "_", I0, ", a), ",&
& I0, ")), ", I0, ")")', advance = 'no') n, n, n, i, -i * n, i
do j = 1, n - 1
write (*, '(")")', advance = 'no')
end do
write (*, '()')
end if
end do
write (*, '(" end function board_reflect3")')
write (*, '()')
end subroutine write_board_reflect3
subroutine write_board_reflect4
integer :: i, j
write (*, '(" elemental function board_reflect4 (a) result (b)")')
write (*, '(" integer(kind = ", A, "), value :: a")') trim (s_kindnxn (n))
write (*, '(" integer(kind = ", A, ") :: b")') trim (s_kindnxn (n))
write (*, '()')
write (*, '(" ! Reflection around one of the two main diagonals.")')
write (*, '()')
do i = 0, n - 1
if (i == 0) then
write (*, '(" b = ")', advance = 'no')
else
write (*, '(" & ")', advance = 'no')
do j = 1, i
write (*, '(" ")', advance = 'no')
end do
end if
if (i /= n - 1) then
write (*, '("ior (ishft (reverse_insert_zeros_", I0, " (ishft&
& (iand (rook1_mask_", I0, "x", I0, "_", I0, ", a), ",&
& I0, ")), ", I0, "), &")') n, n, n, i, -i * n, n - 1 - i
else
write (*, '(" ishft (reverse_insert_zeros_", I0, " (ishft&
& (iand (rook1_mask_", I0, "x", I0, "_", I0, ", a), ",&
& I0, ")), ", I0, ")")', advance = 'no') n, n, n, i, -i * n, n - 1 - i
do j = 1, n - 1
write (*, '(")")', advance = 'no')
end do
write (*, '()')
end if
end do
write (*, '(" end function board_reflect4")')
write (*, '()')
end subroutine write_board_reflect4
subroutine write_insert_zeros
integer :: i, j
write (*, '(" elemental function insert_zeros_", I0, " (a) result (b)")') n
write (*, '(" integer(kind = ", A, "), value :: a")') trim (s_kindnxn (n))
write (*, '(" integer(kind = ", A, ") :: b")') trim (s_kindnxn (n))
write (*, '()')
do i = 0, n - 1
if (i == 0) then
write (*, '(" b = ")', advance = 'no')
else
write (*, '(" & ")', advance = 'no')
do j = 1, i
write (*, '(" ")', advance = 'no')
end do
end if
if (i /= n - 1) then
write (*, '("ior (ishft (ibits (a, ", I0, ", 1), ", I0, "), &")') i, i * n
else
write (*, '("ishft (ibits (a, ", I0, ", 1), ", I0, ")")', advance = 'no') i, i * n
do j = 1, n - 1
write (*, '(")")', advance = 'no')
end do
write (*, '()')
end if
end do
write (*, '(" end function insert_zeros_", I0)') n
write (*, '()')
end subroutine write_insert_zeros
subroutine write_reverse_insert_zeros
integer :: i, j
write (*, '(" elemental function reverse_insert_zeros_", I0, " (a) result (b)")') n
write (*, '(" integer(kind = ", A, "), value :: a")') trim (s_kindnxn (n))
write (*, '(" integer(kind = ", A, ") :: b")') trim (s_kindnxn (n))
write (*, '()')
do i = 0, n - 1
if (i == 0) then
write (*, '(" b = ")', advance = 'no')
else
write (*, '(" & ")', advance = 'no')
do j = 1, i
write (*, '(" ")', advance = 'no')
end do
end if
if (i /= n - 1) then
write (*, '("ior (ishft (ibits (a, ", I0, ", 1), ", I0, "), &")') n - 1 - i, i * n
else
write (*, '("ishft (ibits (a, ", I0, ", 1), ", I0, ")")', advance = 'no') n - 1 - i, i * n
do j = 1, n - 1
write (*, '(")")', advance = 'no')
end do
write (*, '()')
end if
end do
write (*, '(" end function reverse_insert_zeros_", I0)') n
write (*, '()')
end subroutine write_reverse_insert_zeros
function s_kindnxn (n) result (s)
integer, intent(in) :: n
character(len = 16) :: s
if (n <= 8) then
s = s_kind8x8
else
s = s_kind11x11
end if
end function s_kindnxn
subroutine fill_masks (n)
integer, intent(in) :: n
call fill_rook1_masks (n)
call fill_rook2_masks (n)
call fill_bishop1_masks (n)
call fill_bishop2_masks (n)
call fill_rook_masks (n)
call fill_bishop_masks (n)
call fill_queen_masks (n)
end subroutine fill_masks
subroutine fill_rook1_masks (n)
integer, intent(in) :: n
integer :: i
integer(kind = kind11x11) :: mask
mask = (two ** n) - 1
do i = 0, n - 1
rook1_masks(i) = mask
mask = ishft (mask, n)
end do
end subroutine fill_rook1_masks
subroutine fill_rook2_masks (n)
integer, intent(in) :: n
integer :: i
integer(kind = kind11x11) :: mask
mask = 0
do i = 0, n - 1
mask = ior (ishft (mask, n), one)
end do
do i = 0, n - 1
rook2_masks(i) = mask
mask = ishft (mask, 1)
end do
end subroutine fill_rook2_masks
subroutine fill_bishop1_masks (n)
integer, intent(in) :: n
integer :: i, j, k
integer(kind = kind11x11) :: mask0, mask1
! Masks for diagonals. Put them in order from most densely
! populated to least densely populated.
do k = 0, n - 2
mask0 = 0
mask1 = 0
do i = k, n - 1
j = i - k
mask0 = ior (mask0, ishft (one, i + (j * n)))
mask1 = ior (mask1, ishft (one, j + (i * n)))
end do
if (k == 0) then
bishop1_masks(0) = mask0
else
bishop1_masks((2 * k) - 1) = mask0
bishop1_masks(2 * k) = mask1
end if
end do
end subroutine fill_bishop1_masks
subroutine fill_bishop2_masks (n)
integer, intent(in) :: n
integer :: i, j, k
integer :: i1, j1
integer(kind = kind11x11) :: mask0, mask1
! Masks for skew diagonals. Put them in order from most densely
! populated to least densely populated.
do k = 0, n - 2
mask0 = 0
mask1 = 0
do i = k, n - 1
j = i - k
i1 = n - 1 - i
j1 = n - 1 - j
mask0 = ior (mask0, ishft (one, j + (i1 * n)))
mask1 = ior (mask1, ishft (one, i + (j1 * n)))
end do
if (k == 0) then
bishop2_masks(0) = mask0
else
bishop2_masks((2 * k) - 1) = mask0
bishop2_masks(2 * k) = mask1
end if
end do
end subroutine fill_bishop2_masks
subroutine fill_rook_masks (n)
integer, intent(in) :: n
rook_masks(0 : n - 1) = rook1_masks
rook_masks(n : (2 * n) - 1) = rook2_masks
end subroutine fill_rook_masks
subroutine fill_bishop_masks (n)
integer, intent(in) :: n
integer :: i
! Put the masks in order from most densely populated to least
! densely populated.
do i = 0, (2 * n) - 4
bishop_masks(2 * i) = bishop1_masks(i)
bishop_masks((2 * i) + 1) = bishop2_masks(i)
end do
end subroutine fill_bishop_masks
subroutine fill_queen_masks (n)
integer, intent(in) :: n
queen_masks(0 : (2 * n) - 1) = rook_masks
queen_masks(2 * n : (6 * n) - 7) = bishop_masks
end subroutine fill_queen_masks
end program peaceful_queens_elements_generator</syntaxhighlight>
Here is the second program, '''peaceful_queens.f90''':
<syntaxhighlight lang="fortran">module peaceful_queens_support
use, non_intrinsic :: peaceful_queens_elements
implicit none
private
public :: write_board
public :: write_board_without_spaces
public :: write_board_with_spaces
public :: save_a_solution
interface write_board
module procedure write_board_without_spaces
module procedure write_board_with_spaces
end interface write_board
contains
subroutine write_board_without_spaces (unit, army_b, army_w)
integer, intent(in) :: unit
integer(kind = board_kind), intent(in) :: army_b, army_w
call write_board_with_spaces (unit, army_b, army_w, 0)
end subroutine write_board_without_spaces
subroutine write_board_with_spaces (unit, army_b, army_w, num_spaces)
integer, intent(in) :: unit
integer(kind = board_kind), intent(in) :: army_b, army_w
integer, intent(in) :: num_spaces
integer(kind = board_kind), parameter :: zero = 0
integer(kind = board_kind), parameter :: one = 1
integer :: i, j
integer(kind = board_kind) :: rank_b, rank_w
integer(kind = board_kind) :: mask
character(1), allocatable :: queens(:)
character(4), allocatable :: rules(:)
character(1), allocatable :: spaces(:)
allocate (queens(0 : n - 1))
allocate (rules(0 : n - 1))
allocate (spaces(1 : num_spaces))
rules = "----"
if (0 < num_spaces) then
spaces = " " ! For putting spaces after newlines.
end if
mask = not (ishft (not (zero), n))
write (unit, '("+", 100(A4, "+"))') rules
do i = 0, n - 1
rank_b = iand (mask, ishft (army_b, -i * n))
rank_w = iand (mask, ishft (army_w, -i * n))
do j = 0, n - 1
if (iand (rank_b, ishft (one, j)) /= 0) then
queens(j) = "B"
else if (iand (rank_w, ishft (one, j)) /= 0) then
queens(j) = "W"
else
queens(j) = " "
end if
end do
write (unit, '(100A1)', advance = 'no') spaces
write (unit, '("|", 100(A3, " |"))') queens
write (unit, '(100A1)', advance = 'no') spaces
if (i /= n - 1) then
write (unit, '("+", 100(A4, "+"))') rules
else
write (unit, '("+", 100(A4, "+"))', advance = 'no') rules
end if
end do
end subroutine write_board_with_spaces
subroutine save_a_solution (army1, army2, num_solutions, armies1, armies2)
integer(kind = board_kind), intent(in) :: army1, army2
integer, intent(inout) :: num_solutions
integer(kind = board_kind), intent(inout) :: armies1(1:8, 1:max_solutions)
integer(kind = board_kind), intent(inout) :: armies2(1:8, 1:max_solutions)
! A sanity check.
if (queens_attack_check (army1, army2)) then
error stop
end if
num_solutions = num_solutions + 1
armies1(1, num_solutions) = army1
armies1(2, num_solutions) = board_rotate90 (army1)
armies1(3, num_solutions) = board_rotate180 (army1)
armies1(4, num_solutions) = board_rotate270 (army1)
armies1(5, num_solutions) = board_reflect1 (army1)
armies1(6, num_solutions) = board_reflect2 (army1)
armies1(7, num_solutions) = board_reflect3 (army1)
armies1(8, num_solutions) = board_reflect4 (army1)
armies2(1, num_solutions) = army2
armies2(2, num_solutions) = board_rotate90 (army2)
armies2(3, num_solutions) = board_rotate180 (army2)
armies2(4, num_solutions) = board_rotate270 (army2)
armies2(5, num_solutions) = board_reflect1 (army2)
armies2(6, num_solutions) = board_reflect2 (army2)
armies2(7, num_solutions) = board_reflect3 (army2)
armies2(8, num_solutions) = board_reflect4 (army2)
end subroutine save_a_solution
end module peaceful_queens_support
module peaceful_queens_solver
use, non_intrinsic :: peaceful_queens_elements
use, non_intrinsic :: peaceful_queens_support
implicit none
private
public :: solve_peaceful_queens
integer(kind = board_kind), parameter :: zero = 0_board_kind
integer(kind = board_kind), parameter :: one = 1_board_kind
integer(kind = board_kind), parameter :: two = 2_board_kind
contains
subroutine solve_peaceful_queens (unit, show_equivalents, &
& num_solutions, armies1, armies2)
integer, intent(in) :: unit
logical, intent(in) :: show_equivalents
integer, intent(out) :: num_solutions
integer(kind = board_kind), intent(out) :: armies1(1:8, 1:max_solutions)
integer(kind = board_kind), intent(out) :: armies2(1:8, 1:max_solutions)
call solve (zero, 0, 0, zero, 0, 0, 0)
contains
recursive subroutine solve (army1, rooklike11, rooklike12, &
& army2, rooklike21, rooklike22, index)
integer(kind = board_kind), value :: army1
integer, value :: rooklike11, rooklike12
integer(kind = board_kind), value :: army2
integer, value :: rooklike21, rooklike22
integer, value :: index
integer :: num_queens1
integer :: num_queens2
integer(kind = board_kind) :: new_army
integer(kind = board_kind) :: new_army_reversed
integer :: bit1, bit2
logical :: skip
num_queens1 = popcnt (army1)
num_queens2 = popcnt (army2)
if (num_queens1 + num_queens2 == 2 * m) then
if (.not. is_a_duplicate (army1, army2, num_solutions, armies1, armies2)) then
call save_a_solution (army1, army2, num_solutions, armies1, armies2)
write (unit, '("Solution ", I0)') num_solutions
call write_board (unit, army1, army2)
write (unit, '()')
write (unit, '()')
call optionally_write_equivalents
end if
else if (num_queens1 - num_queens2 == 0) then
! It is time to add a queen to army1.
do while (num_solutions < max_solutions .and. index /= n**2)
skip = .false.
new_army = ior (army1, ishft (one, index))
if (new_army == army1) then
skip = .true.
else if (index < n) then
new_army_reversed = board_reflect1 (new_army)
if (new_army_reversed < new_army) then
! Skip a bunch of board_reflect1 equivalents.
skip = .true.
end if
end if
if (skip) then
index = index + 1
else
bit1 = ishft (1, index / n)
bit2 = ishft (1, mod (index, n))
if (iand (rooklike21, bit1) /= 0) then
index = round_up_to_multiple (index + 1, n)
else if (iand (rooklike22, bit2) /= 0) then
index = index + 1
else if (bishops_attack_check (new_army, army2)) then
index = index + 1
else
call solve (new_army, &
& ior (rooklike11, bit1), &
& ior (rooklike12, bit2), &
& army2, rooklike21, rooklike22, &
& n)
index = index + 1
end if
end if
end do
else
! It is time to add a queen to army2.
do while (num_solutions < max_solutions .and. index /= n**2)
new_army = ior (army2, ishft (one, index))
skip = (new_army == army2)
if (skip) then
index = index + 1
else
bit1 = ishft (1, index / n)
bit2 = ishft (1, mod (index, n))
if (iand (rooklike11, bit1) /= 0) then
index = round_up_to_multiple (index + 1, n)
else if (iand (rooklike12, bit2) /= 0) then
index = index + 1
else if (bishops_attack_check (army1, new_army)) then
index = index + 1
else
call solve (army1, rooklike11, rooklike12, &
& new_army, &
& ior (rooklike21, bit1), &
& ior (rooklike22, bit2), &
& 0)
index = index + 1
end if
end if
end do
end if
end subroutine solve
subroutine optionally_write_equivalents
integer :: i
if (show_equivalents) then
write (unit, '(5X)', advance = 'no')
write (unit, '("Equivalents")')
write (unit, '(5X)', advance = 'no')
call write_board (unit, armies2(1, num_solutions), armies1(1, num_solutions), 5)
write (unit, '()')
write (unit, '()')
do i = 2, 5
if (all ((armies1(i, num_solutions) /= armies1(1 : i - 1, num_solutions) .or. &
& armies2(i, num_solutions) /= armies2(1 : i - 1, num_solutions)) .and. &
& (armies2(i, num_solutions) /= armies1(1 : i - 1, num_solutions) .or. &
& armies1(i, num_solutions) /= armies2(1 : i - 1, num_solutions)))) then
write (unit, '(5X)', advance = 'no')
call write_board (unit, armies1(i, num_solutions), armies2(i, num_solutions), 5)
write (unit, '()')
write (unit, '()')
write (unit, '(5X)', advance = 'no')
call write_board (unit, armies2(i, num_solutions), armies1(i, num_solutions), 5)
write (unit, '()')
write (unit, '()')
end if
end do
end if
end subroutine optionally_write_equivalents
end subroutine solve_peaceful_queens
elemental function round_up_to_multiple (x, n) result (y)
integer, value :: x, n
integer :: y
y = x + mod (n - mod (x, n), n)
end function round_up_to_multiple
pure function is_a_duplicate (army1, army2, num_solutions, armies1, armies2) result (is_dup)
integer(kind = board_kind), intent(in) :: army1, army2
integer, intent(in) :: num_solutions
integer(kind = board_kind), intent(in) :: armies1(1:8, 1:max_solutions)
integer(kind = board_kind), intent(in) :: armies2(1:8, 1:max_solutions)
logical :: is_dup
is_dup = any ((army1 == armies1(:, 1:num_solutions) .and. &
& army2 == armies2(:, 1:num_solutions)) .or. &
& (army2 == armies1(:, 1:num_solutions) .and. &
& army1 == armies2(:, 1:num_solutions)))
end function is_a_duplicate
end module peaceful_queens_solver
program peaceful_queens
use, intrinsic :: iso_fortran_env, only: output_unit
use, non_intrinsic :: peaceful_queens_elements
use, non_intrinsic :: peaceful_queens_support
use, non_intrinsic :: peaceful_queens_solver
implicit none
integer :: num_solutions
logical :: show_equivalents
integer(kind = board_kind) :: armies1(1:8, 1:max_solutions)
integer(kind = board_kind) :: armies2(1:8, 1:max_solutions)
integer :: arg_count
character(len = 200) :: arg
show_equivalents = .false.
arg_count = command_argument_count ()
if (1 <= arg_count) then
call get_command_argument (1, arg)
select case (trim (arg))
case ('1', 't', 'T', 'true', 'y', 'Y', 'yes')
show_equivalents = .true.
end select
end if
call solve_peaceful_queens (output_unit, show_equivalents, &
& num_solutions, armies1, armies2)
end program peaceful_queens</syntaxhighlight>
Here is the driver script:
<syntaxhighlight lang="sh">#!/bin/sh
#
# Driver script for peaceful_queens in Fortran.
#
if test ${ZSH_VERSION+y} && (emulate sh) >/dev/null 2>&1; then
emulate sh
fi
if test $# -ne 3 && test $# -ne 4; then
echo "Usage: $0 M N MAX_SOLUTIONS [SHOW_EQUIVALENTS]"
exit 1
fi
M=${1}
N=${2}
MAX_SOLUTIONS=${3}
SHOW_EQUIVALENTS=${4}
RM_GENERATED_SRC=yes
CHECK=no
case ${CHECK} in
0 | f | F | false | N | n | no) FCCHECK="" ;;
1 | t | T | true | Y | y | yes) FCCHECK="-fcheck=all" ;;
*) echo 'CHECK is set incorrectly';
exit 1 ;;
esac
FC="gfortran"
FCFLAGS="-std=f2018 -g -O3 -march=native -fno-stack-protector -Wall -Wextra ${FCCHECK}"
# If you have the graphite optimizer, here are some marginally helpful
# flags. They barely make a difference, for me.
FCFLAGS="${FCFLAGS} -funroll-loops -floop-nest-optimize"
RUN_IT="yes"
${FC} -o peaceful_queens_elements_generator peaceful_queens_elements_generator.f90 &&
./peaceful_queens_elements_generator ${M} ${N} ${MAX_SOLUTIONS} > peaceful_queens_elements.f90 &&
${FC} ${FCFLAGS} -c peaceful_queens_elements.f90 &&
if test x"${RM_GENERATED_SRC}" != xno; then rm -f peaceful_queens_elements.f90; fi &&
${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</syntaxhighlight>
{{out}}
$ ./peaceful_queens-fortran-driver.sh 4 5 1000 T
<pre>Solution 1
+----+----+----+----+----+
| B | | | | B |
+----+----+----+----+----+
| | | W | | |
+----+----+----+----+----+
| | W | | W | |
+----+----+----+----+----+
| | | W | | |
+----+----+----+----+----+
| B | | | | B |
+----+----+----+----+----+
Equivalents
+----+----+----+----+----+
| W | | | | W |
+----+----+----+----+----+
| | | B | | |
+----+----+----+----+----+
| | B | | B | |
+----+----+----+----+----+
| | | B | | |
+----+----+----+----+----+
| W | | | | W |
+----+----+----+----+----+
Solution 2
+----+----+----+----+----+
| B | | | | B |
+----+----+----+----+----+
| | | W | | |
+----+----+----+----+----+
| B | | | | B |
+----+----+----+----+----+
| | | W | | |
+----+----+----+----+----+
| | W | | W | |
+----+----+----+----+----+
Equivalents
+----+----+----+----+----+
| W | | | | W |
+----+----+----+----+----+
| | | B | | |
+----+----+----+----+----+
| W | | | | W |
+----+----+----+----+----+
| | | B | | |
+----+----+----+----+----+
| | B | | B | |
+----+----+----+----+----+
+----+----+----+----+----+
| B | | B | | |
+----+----+----+----+----+
| | | | | W |
+----+----+----+----+----+
| | W | | W | |
+----+----+----+----+----+
| | | | | W |
+----+----+----+----+----+
| B | | B | | |
+----+----+----+----+----+
+----+----+----+----+----+
| W | | W | | |
+----+----+----+----+----+
| | | | | B |
+----+----+----+----+----+
| | B | | B | |
+----+----+----+----+----+
| | | | | B |
+----+----+----+----+----+
| W | | W | | |
+----+----+----+----+----+
+----+----+----+----+----+
| | W | | W | |
+----+----+----+----+----+
| | | W | | |
+----+----+----+----+----+
| B | | | | B |
+----+----+----+----+----+
| | | W | | |
+----+----+----+----+----+
| B | | | | B |
+----+----+----+----+----+
+----+----+----+----+----+
| | B | | B | |
+----+----+----+----+----+
| | | B | | |
+----+----+----+----+----+
| W | | | | W |
+----+----+----+----+----+
| | | B | | |
+----+----+----+----+----+
| W | | | | W |
+----+----+----+----+----+
+----+----+----+----+----+
| | | B | | B |
+----+----+----+----+----+
| W | | | | |
+----+----+----+----+----+
| | W | | W | |
+----+----+----+----+----+
| W | | | | |
+----+----+----+----+----+
| | | B | | B |
+----+----+----+----+----+
+----+----+----+----+----+
| | | W | | W |
+----+----+----+----+----+
| B | | | | |
+----+----+----+----+----+
| | B | | B | |
+----+----+----+----+----+
| B | | | | |
+----+----+----+----+----+
| | | W | | W |
+----+----+----+----+----+
Solution 3
+----+----+----+----+----+
| B | | B | | |
+----+----+----+----+----+
| | | | | W |
+----+----+----+----+----+
| B | | B | | |
+----+----+----+----+----+
| | | | | W |
+----+----+----+----+----+
| | W | | W | |
+----+----+----+----+----+
Equivalents
+----+----+----+----+----+
| W | | W | | |
+----+----+----+----+----+
| | | | | B |
+----+----+----+----+----+
| W | | W | | |
+----+----+----+----+----+
| | | | | B |
+----+----+----+----+----+
| | B | | B | |
+----+----+----+----+----+
+----+----+----+----+----+
| | W | | W | |
+----+----+----+----+----+
| | | | | W |
+----+----+----+----+----+
| B | | B | | |
+----+----+----+----+----+
| | | | | W |
+----+----+----+----+----+
| B | | B | | |
+----+----+----+----+----+
+----+----+----+----+----+
| | B | | B | |
+----+----+----+----+----+
| | | | | B |
+----+----+----+----+----+
| W | | W | | |
+----+----+----+----+----+
| | | | | B |
+----+----+----+----+----+
| W | | W | | |
+----+----+----+----+----+
+----+----+----+----+----+
| | W | | W | |
+----+----+----+----+----+
| W | | | | |
+----+----+----+----+----+
| | | B | | B |
+----+----+----+----+----+
| W | | | | |
+----+----+----+----+----+
| | | B | | B |
+----+----+----+----+----+
+----+----+----+----+----+
| | B | | B | |
+----+----+----+----+----+
| B | | | | |
+----+----+----+----+----+
| | | W | | W |
+----+----+----+----+----+
| B | | | | |
+----+----+----+----+----+
| | | W | | W |
+----+----+----+----+----+
+----+----+----+----+----+
| | | B | | B |
+----+----+----+----+----+
| W | | | | |
+----+----+----+----+----+
| | | B | | B |
+----+----+----+----+----+
| W | | | | |
+----+----+----+----+----+
| | W | | W | |
+----+----+----+----+----+
+----+----+----+----+----+
| | | W | | W |
+----+----+----+----+----+
| B | | | | |
+----+----+----+----+----+
| | | W | | W |
+----+----+----+----+----+
| B | | | | |
+----+----+----+----+----+
| | B | | B | |
+----+----+----+----+----+
</pre>
On my computer, the program can find all the solutions of m=5, n=6, and eliminate any other possibilities, in under 5 seconds. The m=7, n=7 case took about 4.25 hours, mostly eliminating other possibilities. The next thing to try would be m=9, n=8, but probably a faster program is called for, there.
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 2,584 ⟶ 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.
<
import "fmt"
Line 2,708 ⟶ 4,423:
}
}
}</
{{out}}
Line 2,880 ⟶ 4,595:
=={{header|Java}}==
{{trans|Kotlin}}
<
import java.util.Arrays;
import java.util.List;
Line 3,027 ⟶ 4,742:
}
}
}</
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 3,191 ⟶ 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.
<
struct Position
Line 3,299 ⟶ 5,108:
peacefulqueenapp()
</syntaxhighlight>
=={{header|Kotlin}}==
{{trans|D}}
<
enum class Piece {
Line 3,405 ⟶ 5,214:
}
}
}</
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 3,571 ⟶ 5,380:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
VisualizeState[state_] := Module[{q, cells},
q = MapIndexed[If[#["q"] == -1, {}, Text[Style[#["q"], 24], #2]] &, state, {2}];
Line 3,614 ⟶ 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*)</
{{out}}
<pre>[Graphical object]
Line 3,637 ⟶ 5,446:
Almost a direct translation except for "printBoard" where we have chosen to use a sequence of sequences to simplify the code.
<
type
Line 3,710 ⟶ 5,519:
printBoard(n, blackQueens, whiteQueens)
else:
echo "No solution exists.\n"</
{{out}}
Line 3,879 ⟶ 5,688:
=={{header|Perl}}==
===Terse===
<
use warnings;
Line 3,897 ⟶ 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
}</
{{out}}
<pre>Solution to 4 5
Line 3,908 ⟶ 5,717:
===Verbose===
A refactored version of the same code, with fancier output.
<
use warnings;
use feature 'say';
Line 3,959 ⟶ 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";</
{{out}}
<pre>Solution to 4 5
Line 3,972 ⟶ 5,781:
{{trans|Go}}
{{trans|Python}}
You can run this online [http://phix.x10.mx/p2js/QueenArmies.htm here].
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Queen_Armies.exw
-- =============================
--</span>
<span style="color: #008080;">with</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>
<span style="color: #008080;">constant</span> <span style="color: #000000;">as_html</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<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>
<span style="color: #008000;">`♛`</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">`<font color="green">♕</font>`</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">`<span style="color:red">?</span>`</span><span style="color: #0000FF;">}</span>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">as_html</span> <span style="color: #008080;">then</span>
<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;">"<br><b>## %d black and %d white queens on a %d-by-%d board</b><br>\n"</span><span style="color: #0000FF;">,</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: #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>
<span style="color: #000000;">tbl</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
<span style="color: #000000;">out</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">"<table style=\"font-weight:bold\">\n "</span>
<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>
<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>
<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;">" </tr>\n <tr valign=\"middle\" align=\"center\">\n"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<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>
<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>
<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>
<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>
<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>
<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;">" <td style=\"width:14pt; height:14pt;\"%s>%s</td>\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>
<span style="color: #008080;">end</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<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>
<span style="color: #000000;">out</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">" </tr>\n</table>\n<br>\n"</span>
<span style="color: #000000;">html</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">out</span>
<span style="color: #008080;">else</span>
<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>
<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>
<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>
<span style="color: #000080;font-style:italic;">-- ?{n,blackqueens, whitequeens}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<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>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<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>
<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>
<span style="color: #004080;">bool</span> <span style="color: #000000;">placingBlack</span> <span style="color: #0000FF;">:=</span> <span style="color: #004600;">true</span>
<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>
<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>
<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>
<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>
<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>
<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>
<span
<span style="color: #008080;">end</span> <span style="color:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">pos</span><span style="color: #0000FF;">!={}</span> <span style="color: #008080;">then</span>
<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>
<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>
<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>
<span style="color: #000000;">pos</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">end</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">pos</span><span style="color: #0000FF;">!={}</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">placingBlack</span> <span style="color: #008080;">then</span>
<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>
<span style="color: #000000;">placingBlack</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</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>
<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>
<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>
<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>
<span style="color: #000000;">placingBlack</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</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: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<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>
<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>
<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>
<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>
<span style="color: #008080;">if</span> <span style="color: #000000;">as_html</span> <span style="color: #008080;">then</span>
<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;">"<b># %s</b><br><br>\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>
<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>
<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: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">html_header</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Queen Armies</title>
</head>
<body>
<h2>queen armies</h2>
"""</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- or <div style="overflow:scroll; height:250px;"></span>
<span style="color: #000000;">html_footer</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
</body>
</html>
"""</span> <span style="color: #000080;font-style:italic;">-- or </div></span>
<span style="color: #008080;">if</span> <span style="color: #000000;">as_html</span> <span style="color: #008080;">then</span>
<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>
<span style="color: #008080;">else</span>
<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 4,217 ⟶ 6,038:
=={{header|Python}}==
===Python: Textual output===
<
from functools import lru_cache, reduce
Line 4,290 ⟶ 6,111:
m, n = 5, 7
ans = place(m, n)
pboard(ans, n)</
{{out}}
Line 4,390 ⟶ 6,211:
===Python: HTML output===
Uses the solver function <code>place</code> from the above textual output case.
<
from itertools import product, count
Line 4,438 ⟶ 6,259:
html += hboard(ans, n)
with open('peaceful_queen_armies.htm', 'w') as f:
f.write(html)</
{{out}}
Line 5,032 ⟶ 6,853:
(formerly Perl 6)
{{trans|Perl}}
<syntaxhighlight lang="raku"
sub place ($board, $n, $m, $empty-square) {
my $cnt;
Line 5,079 ⟶ 6,900:
say $solution
?? "Solution to $m $n\n\n{S:g/(\N)/$0 / with $solution}"
!! "No solution to $m $n";</
{{out}}
<pre>W • ◦ • W
Line 5,089 ⟶ 6,910:
=={{header|Ruby}}==
{{trans|Java}}
<
attr_reader :x, :y
Line 5,208 ⟶ 7,029:
print "No solution exists.\n\n"
end
end</
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 5,378 ⟶ 7,199:
{{libheader|srfi-132}}
<
;;; Solutions to the Peaceful Chess Queen Armies puzzle, in R7RS
;;; Scheme (using also SRFI-132).
Line 5,607 ⟶ 7,428:
(newline)
(newline)
(loop (+ next-solution-number 1))))))</
{{out}}
Line 5,848 ⟶ 7,669:
===All non-equivalent solutions===
{{works with|CHICKEN|5.3.0}}
<
;;; Solutions to the Peaceful Chess Queen Armies puzzle, in R7RS
;;; Scheme. This implementation returns only one of each equivalent
Line 6,189 ⟶ 8,010:
(newline)
(newline)
(loop (+ next-solution-number 1))))))</
{{out}}
Line 6,237 ⟶ 8,058:
{{trans|Kotlin}}
<
case empty, black, white
}
Line 6,345 ⟶ 8,166:
print("No solution")
}
}</
{{out}}
Line 6,510 ⟶ 8,331:
{{trans|Kotlin}}
{{libheader|Wren-dynamic}}
<
var Piece = Enum.create("Piece", ["empty", "black", "white"])
Line 6,602 ⟶ 8,423:
System.print("No solution exists.\n")
}
}</
{{out}}
Line 6,610 ⟶ 8,431:
=={{header|zkl}}==
<
{ 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 6,642 ⟶ 8,463:
z.text.pump(Void,T(Void.Read,N-1),"println");
}
}</
<
foreach n in ([4..10]){ peacefulQueens(n,n) }</
{{out}}
<pre>
|