summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--90-94/90.hs18
1 files changed, 18 insertions, 0 deletions
diff --git a/90-94/90.hs b/90-94/90.hs
new file mode 100644
index 0000000..a634409
--- /dev/null
+++ b/90-94/90.hs
@@ -0,0 +1,18 @@
+import Data.Bits (shift, (.|.), (.&.), Bits)
+
+queens :: Int -> [[Int]]
+
+queens n = q 0 0 0 0
+ where q :: Int -> Int -> Int -> Int -> [[Int]]
+ q s l r d
+ | s == n = return []
+ | otherwise = do i <- [0..n-1]
+ let p = shift 1 i
+ if p.&.forb == 0 then
+ map (i:) (q (s + 1)
+ (shift (l.|.p) (-1))
+ ((shift (r.|.p) 1).&.mask)
+ (d.|.p))
+ else []
+ where forb = l.|.r.|.d
+ mask = (shift 1 n) - 1