LoginSignup
0
0

More than 5 years have passed since last update.

Haskellでエイトクイーン

Last updated at Posted at 2015-10-21
eightqueens.hs

-- % echo 8 | runhaskell eightqueens.hs

import Control.Applicative
import Data.List

main :: IO ()
main = interact $ unlines . map (>>= show) . (\n -> queens n n [] [] []) . read

queens :: Int -> Int -> [Int] -> [Int] -> [Int] -> [[Int]]
queens _ 0 _ _ _ = [[]]
queens n i ls hs rs = [ q : qs |
        q <- [0 .. n - 1] \\ (ls ++ hs ++ rs),
        qs <- queens n (i - 1) (pred <$> q : ls) (q : hs) (succ <$> q : rs) ]

あとで説明とgithubへのリンクをつけよう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0