Data update
This commit is contained in:
parent
4d5544505c
commit
4924dd0264
3073 changed files with 55820 additions and 4408 deletions
32
Task/N-queens-problem/ArkScript/n-queens-problem.ark
Normal file
32
Task/N-queens-problem/ArkScript/n-queens-problem.ark
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
(let queenPuzzle (fun (rows columns)
|
||||
(if (<= rows 0)
|
||||
[[]]
|
||||
(addQueen (- rows 1) columns))))
|
||||
|
||||
(let addQueen (fun (newRow columns) {
|
||||
(mut newSolutions [])
|
||||
(let prev (queenPuzzle newRow columns))
|
||||
|
||||
(mut i 0)
|
||||
(while (< i (len prev)) {
|
||||
(let solution (@ prev i))
|
||||
(mut newColumn 0)
|
||||
(while (< newColumn columns) {
|
||||
(if (not (hasConflict newRow newColumn solution)) (append! newSolutions (append solution newColumn)))
|
||||
(set newColumn (+ newColumn 1)) })
|
||||
|
||||
(set i (+ i 1)) })
|
||||
|
||||
newSolutions }))
|
||||
|
||||
(let hasConflict (fun (newRow newColumn solution) {
|
||||
(mut i 0)
|
||||
(mut conflict false)
|
||||
(while (and (< i newRow) (not conflict)) {
|
||||
(if (or (= (@ solution i) newColumn) (= (+ (@ solution i) i) (+ newColumn newRow)) (= (- (@ solution i) i) (- newColumn newRow)))
|
||||
(set conflict true))
|
||||
(set i (+ i 1)) })
|
||||
|
||||
conflict }))
|
||||
|
||||
(print (queenPuzzle 4 4))
|
||||
Loading…
Add table
Add a link
Reference in a new issue