Data update
This commit is contained in:
parent
81fd053722
commit
52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions
|
|
@ -1,48 +1,31 @@
|
|||
templates queens
|
||||
def n: $;
|
||||
templates getRowColumn
|
||||
when <?($@queens.freeRows($.r::raw) <=0>)> do 0 !
|
||||
when <?($@queens.freeMaxs($.r::raw + $.c::raw) <=0>)> do 0 !
|
||||
when <?($@queens.freeMins($.c::raw - $.r::raw + $n) <=0>)> do 0 !
|
||||
otherwise 1!
|
||||
end getRowColumn
|
||||
|
||||
sink setRowColumn
|
||||
def p: $;
|
||||
@queens.freeRows($p.r::raw): $p.val::raw;
|
||||
@queens.freeMaxs($p.c::raw + $p.r::raw): $p.val::raw;
|
||||
@queens.freeMins($p.c::raw - $p.r::raw + $n): $p.val::raw;
|
||||
end setRowColumn
|
||||
|
||||
data done <=1>
|
||||
|
||||
templates placeQueen
|
||||
def c: $;
|
||||
row´1 -> #
|
||||
when <done> do 1!
|
||||
when <=row´($n+1)> do 0 !
|
||||
when <?({r: $, c: $c} -> getRowColumn <=1>)> do
|
||||
def r: $;
|
||||
@queens.queenRows($r::raw): $c;
|
||||
{r: $, c: $c, val: 0} -> !setRowColumn
|
||||
$c -> \(<=col´$n> done´1!
|
||||
<?(col´($c::raw + 1) -> placeQueen <=1>)> done´1!
|
||||
<>
|
||||
{r: $r, c: $c, val: 1} -> !setRowColumn
|
||||
row´($r::raw + 1) !\) -> #
|
||||
otherwise row´($::raw + 1) -> #
|
||||
end placeQueen
|
||||
|
||||
@: { freeRows: [1..$n -> 1],
|
||||
freeMaxs: [1..$n*2 -> 1],
|
||||
freeMins: [1..$n*2 -> 1],
|
||||
queenRows: [1..$n -> -1] };
|
||||
col´1 -> placeQueen -> \(<=1> $@queens.queenRows ! <> 'non-existent'!\)!
|
||||
queens templates
|
||||
n is $;
|
||||
addColumn templates
|
||||
prev is $;
|
||||
addIfPossible templates
|
||||
row is $;
|
||||
minor is $ - $prev::length - 1;
|
||||
major is $ + $prev::length + 1;
|
||||
-- If prev is not an array that contains row, send it on...
|
||||
$prev -> if <~|[<|=$row>]>
|
||||
-> if <|?($(.. as i; -> $ - $i) matches <~|[<|=$minor>]>)>
|
||||
-> if <|?($(.. as i; -> $ + $i) matches <~|[<|=$major>]>)>
|
||||
-> [ $..., $row] !
|
||||
end addIfPossible
|
||||
1..$n -> addIfPossible !
|
||||
end addColumn
|
||||
1..$n -> [$] -> # !
|
||||
when <|[](=$n)> do $ !
|
||||
otherwise $ -> addColumn -> # !
|
||||
end queens
|
||||
|
||||
'A solution to the 8 queens problem is $:8 -> queens;
|
||||
' -> !OUT::write
|
||||
'A solution to the 4 queens problem is $:4 -> queens;
|
||||
' -> !OUT::write
|
||||
'A solution to the 3 queens problem is $:3 -> queens;
|
||||
' -> !OUT::write
|
||||
solutions is [ 8 -> queens ];
|
||||
'For 8 queens there are $solutions::length; solutions
|
||||
' !
|
||||
|
||||
columns is ['abcdefgh'...];
|
||||
'One of them is $solutions(1) -> $(.. as i; -> '$columns($i);$;');
|
||||
' !
|
||||
|
||||
'For 3 queens there are $:[3 -> queens] -> $::length; solutions
|
||||
' !
|
||||
|
|
|
|||
48
Task/N-queens-problem/Tailspin/n-queens-problem-3.tailspin
Normal file
48
Task/N-queens-problem/Tailspin/n-queens-problem-3.tailspin
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
templates queens
|
||||
def n: $;
|
||||
templates getRowColumn
|
||||
when <?($@queens.freeRows($.r::raw) <=0>)> do 0 !
|
||||
when <?($@queens.freeMaxs($.r::raw + $.c::raw) <=0>)> do 0 !
|
||||
when <?($@queens.freeMins($.c::raw - $.r::raw + $n) <=0>)> do 0 !
|
||||
otherwise 1!
|
||||
end getRowColumn
|
||||
|
||||
sink setRowColumn
|
||||
def p: $;
|
||||
@queens.freeRows($p.r::raw): $p.val::raw;
|
||||
@queens.freeMaxs($p.c::raw + $p.r::raw): $p.val::raw;
|
||||
@queens.freeMins($p.c::raw - $p.r::raw + $n): $p.val::raw;
|
||||
end setRowColumn
|
||||
|
||||
data done <=1>
|
||||
|
||||
templates placeQueen
|
||||
def c: $;
|
||||
row´1 -> #
|
||||
when <done> do 1!
|
||||
when <=row´($n+1)> do 0 !
|
||||
when <?({r: $, c: $c} -> getRowColumn <=1>)> do
|
||||
def r: $;
|
||||
@queens.queenRows($r::raw): $c;
|
||||
{r: $, c: $c, val: 0} -> !setRowColumn
|
||||
$c -> \(<=col´$n> done´1!
|
||||
<?(col´($c::raw + 1) -> placeQueen <=1>)> done´1!
|
||||
<>
|
||||
{r: $r, c: $c, val: 1} -> !setRowColumn
|
||||
row´($r::raw + 1) !\) -> #
|
||||
otherwise row´($::raw + 1) -> #
|
||||
end placeQueen
|
||||
|
||||
@: { freeRows: [1..$n -> 1],
|
||||
freeMaxs: [1..$n*2 -> 1],
|
||||
freeMins: [1..$n*2 -> 1],
|
||||
queenRows: [1..$n -> -1] };
|
||||
col´1 -> placeQueen -> \(<=1> $@queens.queenRows ! <> 'non-existent'!\)!
|
||||
end queens
|
||||
|
||||
'A solution to the 8 queens problem is $:8 -> queens;
|
||||
' -> !OUT::write
|
||||
'A solution to the 4 queens problem is $:4 -> queens;
|
||||
' -> !OUT::write
|
||||
'A solution to the 3 queens problem is $:3 -> queens;
|
||||
' -> !OUT::write
|
||||
Loading…
Add table
Add a link
Reference in a new issue