Created
April 11, 2017 16:22
-
-
Save gdejohn/6649695ea2083ac324156e1c2b9c5c7f to your computer and use it in GitHub Desktop.
Enumerate solutions to the n queens problem
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
import Control.Applicative (liftA2) | |
import Data.List (inits, permutations) | |
queens :: Int -> [[(Int, Int)]] | |
queens n = filter p $ liftA2 map zip permutations $ take n [0 ..] | |
where p x = and $ zipWith f x $ inits x | |
f (a, b) = all $ \(c, d) -> abs (a - c) /= abs (b - d) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment