Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
24
Task/N-queens-problem/Raku/n-queens-problem.raku
Normal file
24
Task/N-queens-problem/Raku/n-queens-problem.raku
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
sub MAIN(\N = 8) {
|
||||
sub collision(@field, $row) {
|
||||
for ^$row -> $i {
|
||||
my $distance = @field[$i] - @field[$row];
|
||||
return True if $distance == any(0, $row - $i, $i - $row);
|
||||
}
|
||||
False;
|
||||
}
|
||||
sub search(@field, $row) {
|
||||
return @field if $row == N;
|
||||
for ^N -> $i {
|
||||
@field[$row] = $i;
|
||||
return search(@field, $row + 1) || next
|
||||
unless collision(@field, $row);
|
||||
}
|
||||
()
|
||||
}
|
||||
for 0 .. N / 2 {
|
||||
if search [$_], 1 -> @f {
|
||||
say @f;
|
||||
last;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue