June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
24
Task/N-queens-problem/Wart/n-queens-problem.wart
Normal file
24
Task/N-queens-problem/Wart/n-queens-problem.wart
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
def (nqueens n queens)
|
||||
prn "step: " queens # show progress
|
||||
if (len.queens = n)
|
||||
prn "solution! " queens
|
||||
# else
|
||||
let row (if queens (queens.zero.zero + 1) 0)
|
||||
for col 0 (col < n) ++col
|
||||
let new_queens (cons (list row col) queens)
|
||||
if (no conflicts.new_queens)
|
||||
(nqueens n new_queens)
|
||||
|
||||
# check if the first queen in 'queens' lies on the same column or diagonal as
|
||||
# any of the others
|
||||
def (conflicts queens)
|
||||
let (curr ... rest) queens
|
||||
or (let curr_column curr.1
|
||||
(some (fn(_) (= _ curr_column))
|
||||
(map cadr rest))) # columns
|
||||
(some (fn(_) (diagonal_match curr _))
|
||||
rest)
|
||||
|
||||
def (diagonal_match curr other)
|
||||
(= (abs (curr.0 - other.0))
|
||||
(abs (curr.1 - other.1)))
|
||||
Loading…
Add table
Add a link
Reference in a new issue