home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 5 Edit
/
05-Edit.zip
/
me34exe.zip
/
mutt
/
package
/
queen.mut
< prev
next >
Wrap
Lisp/Scheme
|
1995-01-14
|
2KB
|
83 lines
;; place N queens on a NxN board
;; C Durland Public Domain
(include me.mh)
(array small-int boardx 20 boardy 20)
(small-int nQueens N)
(defun
put-queen (int x y)(s)
{
(move-cursor (+ y 1) (+ 1 (* x 2)) (concat s));(update)
}
print-board
{
(move-cursor 0 0 ".---------------.")
(move-cursor 1 0 "|*|*|*|*|*|*|*|*|")
(move-cursor 2 0 "|*|*|*|*|*|*|*|*|")
(move-cursor 3 0 "|*|*|*|*|*|*|*|*|")
(move-cursor 4 0 "|*|*|*|*|*|*|*|*|")
(move-cursor 5 0 "|*|*|*|*|*|*|*|*|")
(move-cursor 6 0 "|*|*|*|*|*|*|*|*|")
(move-cursor 7 0 "|*|*|*|*|*|*|*|*|")
(move-cursor 8 0 "|*|*|*|*|*|*|*|*|")
(move-cursor 9 0 "`---------------'")
}
)
(defun
print-solution
{
(int j)
(for (j 0)(< j N)(+= j 1) (msg "(" (boardx j) "," (boardy j) ")"))
}
threat (int a b x y)
{
(or
(== a x)(== b y)
(== (- a b)(- x y))
(== (+ a b)(+ x y))
)
}
conflict (int x y)
{
(int n)
(for (n 0)(< n nQueens)(+= n 1)
(if (threat x y (boardx n) (boardy n)) { TRUE (done) }))
FALSE
}
fill-board (int x y)
{
(int i j z)(z x)
(for (i z)(< i N)(+= i 1)
{
(for (j z)(< j N)(+= j 1)
{
(if (conflict i j) ()
{
(put-queen i j nQueens)
(boardx nQueens i)(boardy nQueens j)(+= nQueens 1)
(fill-board i j)
(if (== nQueens N){ TRUE (done) })
(-= nQueens 1)(put-queen i j ".")
})
})
(z 0)
})
FALSE
}
Queens
{
(N (convert-to NUMBER (ask "Number of queens (max 8) = ")))
(if (or (> N 8) (< N 1)) { (msg "Bogus number of queens")(done) })
(nQueens 0)(print-board)
(if (fill-board 0 0) { (msg "done") (get-key) }
(msg "No solution"))
}
)
(defun MAIN { (Queens) })