Peaceful chess queen armies: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|Phix}}: syntax coloured, added online link) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 58:
{{trans|D}}
<
EMPTY
BLACK
Line 138:
printBoard(nm[0], blackQueens, whiteQueens)
E
print("No solution exists.\n")</
{{out}}
Line 316:
(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.)
<
#define ATS_DYNLOADFLAG 0
Line 1,260:
end
(********************************************************************)</
{{out}}
Line 1,306:
=={{header|C}}==
{{trans|C#}}
<
#include <stdbool.h>
#include <stdio.h>
Line 1,578:
return EXIT_SUCCESS;
}</
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 1,745:
=={{header|C sharp|C#}}==
{{trans|D}}
<
using System.Collections.Generic;
Line 1,858:
}
}
}</
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 2,025:
=={{header|C++}}==
{{trans|D}}
<
#include <vector>
Line 2,139:
return 0;
}</
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 2,306:
=={{header|D}}==
{{trans|Go}}
<
import std.math;
import std.stdio;
Line 2,417:
}
}
}</
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 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:
<
use, intrinsic :: iso_fortran_env, only: int64
use, intrinsic :: iso_fortran_env, only: error_unit
Line 3,407:
end subroutine fill_queen_masks
end program peaceful_queens_elements_generator</
Here is the second program, '''peaceful_queens.f90''':
<
use, non_intrinsic :: peaceful_queens_elements
Line 3,722:
& num_solutions, armies1, armies2)
end program peaceful_queens</
Here is the driver script:
<
#
# Driver script for peaceful_queens in Fortran.
Line 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</
{{out}}
Line 4,004:
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 4,128:
}
}
}</
{{out}}
Line 4,300:
=={{header|Java}}==
{{trans|Kotlin}}
<
import java.util.Arrays;
import java.util.List;
Line 4,447:
}
}
}</
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 4,614:
=={{header|Julia}}==
GUI version, uses the Gtk library. The place! function is condensed from the C# example.
<
struct Position
Line 4,719:
peacefulqueenapp()
</syntaxhighlight>
=={{header|Kotlin}}==
{{trans|D}}
<
enum class Piece {
Line 4,825:
}
}
}</
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 4,991:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
VisualizeState[state_] := Module[{q, cells},
q = MapIndexed[If[#["q"] == -1, {}, Text[Style[#["q"], 24], #2]] &, state, {2}];
Line 5,034:
]
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 5,057:
Almost a direct translation except for "printBoard" where we have chosen to use a sequence of sequences to simplify the code.
<
type
Line 5,130:
printBoard(n, blackQueens, whiteQueens)
else:
echo "No solution exists.\n"</
{{out}}
Line 5,299:
=={{header|Perl}}==
===Terse===
<
use warnings;
Line 5,317:
(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 5,328:
===Verbose===
A refactored version of the same code, with fancier output.
<
use warnings;
use feature 'say';
Line 5,379:
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 5,393:
{{trans|Python}}
You can run this online [http://phix.x10.mx/p2js/QueenArmies.htm here].
<!--<
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Queen_Armies.exw
Line 5,531:
<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>
<!--</
{{out}}
with as_html = false
Line 5,649:
=={{header|Python}}==
===Python: Textual output===
<
from functools import lru_cache, reduce
Line 5,722:
m, n = 5, 7
ans = place(m, n)
pboard(ans, n)</
{{out}}
Line 5,822:
===Python: HTML output===
Uses the solver function <code>place</code> from the above textual output case.
<
from itertools import product, count
Line 5,870:
html += hboard(ans, n)
with open('peaceful_queen_armies.htm', 'w') as f:
f.write(html)</
{{out}}
Line 6,464:
(formerly Perl 6)
{{trans|Perl}}
<syntaxhighlight lang="raku"
sub place ($board, $n, $m, $empty-square) {
my $cnt;
Line 6,511:
say $solution
?? "Solution to $m $n\n\n{S:g/(\N)/$0 / with $solution}"
!! "No solution to $m $n";</
{{out}}
<pre>W • ◦ • W
Line 6,521:
=={{header|Ruby}}==
{{trans|Java}}
<
attr_reader :x, :y
Line 6,640:
print "No solution exists.\n\n"
end
end</
{{out}}
<pre>1 black and 1 white queens on a 2 x 2 board:
Line 6,810:
{{libheader|srfi-132}}
<
;;; Solutions to the Peaceful Chess Queen Armies puzzle, in R7RS
;;; Scheme (using also SRFI-132).
Line 7,039:
(newline)
(newline)
(loop (+ next-solution-number 1))))))</
{{out}}
Line 7,280:
===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 7,621:
(newline)
(newline)
(loop (+ next-solution-number 1))))))</
{{out}}
Line 7,669:
{{trans|Kotlin}}
<
case empty, black, white
}
Line 7,777:
print("No solution")
}
}</
{{out}}
Line 7,942:
{{trans|Kotlin}}
{{libheader|Wren-dynamic}}
<
var Piece = Enum.create("Piece", ["empty", "black", "white"])
Line 8,034:
System.print("No solution exists.\n")
}
}</
{{out}}
Line 8,042:
=={{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 8,074:
z.text.pump(Void,T(Void.Read,N-1),"println");
}
}</
<
foreach n in ([4..10]){ peacefulQueens(n,n) }</
{{out}}
<pre>
|