-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueens.hs
36 lines (24 loc) · 1.01 KB
/
Queens.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
type Queen = (Int, Int)
queensAttack :: Queen -> Queen -> Bool
queensAttack (p1, p2) (q1, q2) = or [p1 == q1,
p2 == q2,
(abs (p1 - q1)) == (abs (p2 - q2))]
newQueen :: [Queen] -> Queen -> Bool
newQueen qs q = not $ or (map (queensAttack q) qs)
numsTo :: Int -> [Int]
numsTo n
| n > 0 = [0..(n - 1)]
| otherwise = []
countSol :: Int -> [Queen] -> Int
countSol 0 _ = 0
countSol n qs
| (length qs) == (n - 1) = if or (map (\i -> newQueen qs (n - 1, i))
(numsTo n)) then 1 else 0
| otherwise = sum $ map (\i -> countSol n ((length qs, i):qs)) (
filter (\i -> newQueen qs (length qs, i))
(numsTo n))
main :: IO ()
main = do putStrLn "Enter Number of Queens:"
num <- getLine
n <- return $ read num
putStrLn . show $ countSol n []