diff options
author | Steven <steven.vasilogianis@gmail.com> | 2019-06-04 23:00:01 -0400 |
---|---|---|
committer | Steven <steven.vasilogianis@gmail.com> | 2019-06-04 23:00:01 -0400 |
commit | 17a1785bbd3efa87995f2e8226aef6763e3c1971 (patch) | |
tree | 9dfbb713c0da6256482eb94269a241a374dcff61 | |
parent | 0f3dad2819eb6664c44e210308e384ccfc1a402a (diff) |
Revert
This reverts commit 0f3dad2819eb6664c44e210308e384ccfc1a402a.
-rw-r--r-- | src/Main.hs | 42 |
1 files changed, 17 insertions, 25 deletions
diff --git a/src/Main.hs b/src/Main.hs index 4d04d06..190e997 100644 --- a/src/Main.hs +++ b/src/Main.hs | |||
@@ -18,28 +18,20 @@ solve n = nub $ solveN n | |||
18 | solveN 1 = solve' [initBoard] | 18 | solveN 1 = solve' [initBoard] |
19 | solveN x = solve' (solveN (x-1)) | 19 | solveN x = solve' (solveN (x-1)) |
20 | solve' bs = concatMap (\bn -> map ((`placeQueen` bn)) (openPositions bn)) bs | 20 | solve' bs = concatMap (\bn -> map ((`placeQueen` bn)) (openPositions bn)) bs |
21 | 21 | initBoard = matrix 8 8 (const O) | |
22 | initBoard :: Matrix Square | 22 | openPositions b = [(i,j) | i <- [1..8], j <- [1..8], b ! (i,j) == O ] |
23 | initBoard = matrix 8 8 (const O) | 23 | placeQueen p b = mark Qu p $ markAttacks b |
24 | 24 | where | |
25 | placeQueen :: (Int, Int) -> Matrix Square -> Matrix Square | 25 | markAttacks bo = markAll X positions bo |
26 | placeQueen p b = setElem Qu p $ markAttacks p b | 26 | positions = concatMap walk directions |
27 | 27 | step (fx,fy) = (\(k,l) -> (fx k, fy l)) | |
28 | markAttacks :: (Int, Int) -> Matrix Square -> Matrix Square | 28 | walk s = takeWhile inBounds $ iterate (step s) $ step s p |
29 | markAttacks p b = markAll positions b | 29 | directions = let a = (+1); s = subtract 1; nc = id |
30 | where markAll [] mb = mb | 30 | in [ (a,nc),(s,nc) -- up, down |
31 | markAll (mp:mps) mb = let nb = setElem X mp mb | 31 | , (nc,a),(nc,s) -- right, left |
32 | in markAll mps nb | 32 | , (a,a),(a,s),(s,a),(s,s) ] -- diags |
33 | positions = concatMap walk stepDirections | 33 | inBounds (i,j) = i >= 1 && i <= nrows b && j >= 1 && j <= ncols b |
34 | where walk dir = takeWhile inBounds $ iterate (dir) $ dir p | 34 | markAll _ [] mb = mb |
35 | inBounds (i,j) = i >= 1 && i <= nrows b && | 35 | markAll x (mp:mps) mb = let nb = mark x mp mb |
36 | j >= 1 && j <= ncols b | 36 | in markAll x mps nb |
37 | stepDirections = | 37 | mark x mp mb = setElem x mp mb |
38 | let a = (+1); s = subtract 1; k = id -- add; subtract; keep | ||
39 | dirF (fr,fc) = (\(r,c) -> (fr r, fc c)) | ||
40 | in map dirF [ (a,k), (s,k) -- up, down | ||
41 | , (k,a),(k,s) -- right, left | ||
42 | , (a,a),(a,s),(s,a),(s,s) ] -- diagonals | ||
43 | |||
44 | openPositions :: Matrix Square -> [(Int, Int)] | ||
45 | openPositions b = [(r,c) | r <- [1..8], c <- [1..8], b ! (r,c) == O ] | ||