-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncode.hs
29 lines (23 loc) · 1.06 KB
/
Encode.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
module Encode where
import Data.List (tails)
import Grid
import DPLL (Literal (..), CNF (..), neg)
-- If a cell (i, j) contains the number n, then ijn, else -ijn
-- http://www.cs.qub.ac.uk/~I.Spence/SuDoku/SuDoku.html
enc :: Grid -> CNF (Int, Int, Int)
enc g = prefilled g ++ noMoreThanOne ++ atLeastOne
-- Pre-filled cells
prefilled :: Grid -> CNF (Int, Int, Int)
prefilled g = [[P (i, j, n)] |
(i, row) <- zip [1..] g,
(j, n) <- zip [1..] row,
n /= 0]
-- Below encoding stays the same for all grids
atLeastOne :: CNF (Int, Int, Int)
atLeastOne = [[P (i, j, n) | n <- [1..9]] | i <- [1..9], j <- [1..9]] -- Cells
++ [[P (i, j, n) | j <- [1..9]] | i <- [1..9], n <- [1..9]] -- Rows
++ [[P (i, j, n) | i <- [1..9]] | j <- [1..9], n <- [1..9]] -- Columns
++ [[P (i, j, n) | i <- [ii..ii + 2], j <- [jj..jj + 2]] |
ii <- [1,4,7], jj <- [1,4,7], n <- [1..9]] -- Blocks
noMoreThanOne :: CNF (Int, Int, Int)
noMoreThanOne = [[neg x, neg y] | row <- atLeastOne, (x:xs) <- tails row, y <- xs]