Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.Set hiding (foldr, map)
- data Fig = Q | R | B | K | N
- deriving (Eq, Ord)
- type Cell = (Int, Int)
- type Placed = (Cell, Fig)
- fac n = foldr (*) 1 [1 .. n]
- layoutsCount :: Int -> Int -> [(Int, Fig)] -> Int
- layoutsCount m n f = length (layouts [] allCells ungroup) `quot` repeats where
- ungroup = concatMap (uncurry replicate) f
- repeats = product $ map (fac . fst) f
- allCells = fromList [(x, y) | x <- [1 .. m], y <- [1 .. n]]
- layouts :: [Placed] -> Set Cell -> [Fig] -> [[Placed]]
- layouts cur _ [] = [cur]
- layouts cur cells (f:fs) = concat
- [ layouts ((c, f):cur) (cells \\ nc) fs
- | c <- toList cells
- , let nc = fromList $ attacked c f
- , all (flip notMember nc . fst) cur
- ]
- attacked c Q = attacked c R ++ attacked c B
- attacked (x, y) R = [(i, y) | i <- [1 .. m]] ++ [(x, j) | j <- [1 .. n]]
- attacked (x, y) B = [(i, i - o) | let o = x - y, i <- [max 1 (1 + o) .. min m (n + o)]]
- ++ [(i, o - i) | let o = x + y, i <- [max 1 (o - n) .. min m (o - 1)]]
- attacked (x, y) K = [(x + i, y + j) | i <- [-1 .. 1], j <- [-1 .. 1]]
- attacked (x, y) N = (x, y):[(x + i, y + j) | i <- [-1, 1], j <- [-2, 2]]
- ++ [(x + i, y + j) | i <- [-2, 2], j <- [-1, 1]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement