Created
January 1, 2014 10:05
99 questions No.90
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
generate :: Int -> [[Int]] | |
generate 0 = [[]] | |
generate n = iter [1..n] [] where | |
iter [] ys = return (reverse ys) | |
iter xs ys = do | |
x <- xs | |
iter (filter (/=x) xs) (x:ys) | |
queens :: Int -> [[Int]] | |
queens n = filter test $ generate n | |
test :: [Int] -> Bool | |
test [] = True | |
test (i:is) = not (check 1 i is) && test is | |
check :: Int -> Int -> [Int] -> Bool | |
check _ _ [] = False | |
check n j (i:is) = | |
if j == i + n || j == i - n | |
then True | |
else check (n+1) j is |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment