Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.List
- import Data.Array
- import qualified Data.Set as S
- import qualified Data.Map as M
- main = interact $ format . solve . parse
- format True = "POSSIBLE"
- format False = "IMPOSSIBLE"
- solve g0 = go S.empty [g0] where
- go _ [] = False
- go cl (s:q) | s `S.member` cl = go cl q
- | elems s == "12345678." = True
- | otherwise = go (S.insert s cl) (next s ++ q)
- next g = map (move g hole) (adj hole) where
- Just hole = find ((== '.') . (g!)) (indices g)
- move g dst src = g // [(src,'.'),(dst,g!src)]
- adj (1,1) = [(2,3),(3,2)]
- adj (1,2) = [(3,1),(3,3)]
- adj (1,3) = [(2,1),(3,2)]
- adj (2,1) = [(1,3),(3,3)]
- adj (2,2) = []
- adj (2,3) = [(1,1),(3,1)]
- adj (3,1) = [(1,2),(2,3)]
- adj (3,2) = [(1,1),(1,3)]
- adj (3,3) = [(1,2),(2,1)]
- parse = listArray ((1,1),(3,3)) . concat . lines
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement