N-queens problem: Difference between revisions
→{{header|Perl 6}}: added section
(changed lang tag for Clojure) |
(→{{header|Perl 6}}: added section) |
||
Line 1,509:
There is a more concise and much more efficient [http://www.mozart-oz.org/documentation/fdt/node25.html#section.scripts.queens solution] in the Mozart documentation.
=={{header|Perl 6}}==
Neither pretty nor efficient, a simple backtracking solution
<lang perl6>
sub MAIN($N = 8) {
sub collision(@field, $row) {
for ^$row -> $i {
my $distance = @field[$i] - @field[$row];
return 1 if $distance == any(0, $row - $i, $i - $row);
}
0;
}
sub search(@field is rw, $row) {
if $row == $N {
return @field;
} else {
for ^$N -> $i {
@field[$row] = $i;
if !collision(@field, $row) {
my @r = search(@field, $row + 1);
return @r if @r;
}
}
}
return;
}
my @field;
for (0..$N/2) {
my @f = search [$_], 1;
if @f {
say ~@f;
last;
}
}
}
# output:
0 4 7 5 2 6 1 3
</lang>
=={{header|PicoLisp}}==
|