tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
29
Task/N-queens-problem/Forth/n-queens-problem.fth
Normal file
29
Task/N-queens-problem/Forth/n-queens-problem.fth
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
variable solutions
|
||||
variable nodes
|
||||
|
||||
: bits ( n -- mask ) 1 swap lshift 1- ;
|
||||
: lowBit ( mask -- bit ) dup negate and ;
|
||||
: lowBit- ( mask -- bits ) dup 1- and ;
|
||||
|
||||
: next3 ( dl dr f files -- dl dr f dl' dr' f' )
|
||||
invert >r
|
||||
2 pick r@ and 2* 1+
|
||||
2 pick r@ and 2/
|
||||
2 pick r> and ;
|
||||
|
||||
: try ( dl dr f -- )
|
||||
dup if
|
||||
1 nodes +!
|
||||
dup 2over and and
|
||||
begin ?dup while
|
||||
dup >r lowBit next3 recurse r> lowBit-
|
||||
repeat
|
||||
else 1 solutions +! then
|
||||
drop 2drop ;
|
||||
|
||||
: queens ( n -- )
|
||||
0 solutions ! 0 nodes !
|
||||
-1 -1 rot bits try
|
||||
solutions @ . ." solutions, " nodes @ . ." nodes" ;
|
||||
|
||||
8 queens \ 92 solutions, 1965 nodes
|
||||
Loading…
Add table
Add a link
Reference in a new issue