Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import qualified Data.Matrix as Matrix
  2. import qualified Data.Vector as Vector
  3. import Data.Typeable
  4. import Debug.Trace
  5. import qualified Data.Maybe as Maybe
  6. import qualified Control.Monad as Monad
  7.  
  8. -- inp = Matrix.fromLists [[0, 5, 8, 10, 0, 0, 3, 6],
  9. --                     [5, 0, 2, 0, 0, 0, 0, 1],
  10. --                     [8, 2, 0, 4, 5, 6, 0, 7],
  11. --                     [10, 0, 4, 0, 12, 9, 7, 0],
  12. --                     [0, 0, 5, 12, 0, 9, 0, 12],
  13. --                     [0, 0, 6, 9, 9, 0, 8, 0],
  14. --                     [3, 0, 0, 7, 0, 8, 0, 2],
  15. --                     [6, 1, 7, 0, 12, 0, 2, 0]]
  16.  
  17. parseLine :: IO [Int]
  18. parseLine = do
  19.     line <- getLine
  20.     map read (words line)
  21.  
  22. consoleInput :: IO [[Int]]
  23. consoleInput = do
  24.     input <- getLine
  25.     Monad.replicateM (read input :: Int) parseLine
  26.  
  27. -- parseInput :: [String] -> [[Int]]
  28. -- parseInput =
  29. --     map
  30. --     (\line ->
  31. --         map
  32. --         read
  33. --         (words line)
  34. --     )
  35.  
  36. matrixShift :: Matrix.Matrix Int -> Matrix.Matrix (Maybe Int)
  37. matrixShift mat =
  38.     Matrix.matrix
  39.     (Matrix.nrows mat)
  40.     (Matrix.ncols mat)
  41.     (\(i,j) ->
  42.         let elem = Matrix.getElem i j mat
  43.         in
  44.         if elem == 0 then
  45.             Nothing
  46.         else
  47.             Just elem
  48.     )
  49.  
  50. -- matrRead :: [[ Int ]]
  51. -- matrRead = read
  52.  
  53. main = do
  54.     mm <- consoleInput
  55.     let inp = Matrix.fromLists mm
  56.     print inp
  57.     print $ solve (matrixShift inp)
  58.  
  59.  
  60.  
  61. folding :: Maybe([Int], Int) -> Maybe([Int], Int) -> Maybe([Int], Int)
  62. folding (Just sum) (Just curr) =
  63.     if snd(sum) < snd(curr) then
  64.         Just sum
  65.     else
  66.         Just curr
  67.  
  68. folding Nothing (Just curr) =
  69.     Just curr
  70.  
  71. folding Nothing Nothing =
  72.     Nothing
  73.  
  74. folding (Just sum) Nothing =
  75.     Just sum
  76.  
  77. enumerate :: [Maybe Int] -> [(Int, Maybe Int)]
  78. enumerate row =
  79.     map (\(x, y) -> ((fromInteger x), y)) (zip [1..] row)
  80.  
  81. solve_step :: Matrix.Matrix(Maybe Int) -> [Int] -> Int -> Int -> Maybe ([Int], Int)
  82. solve_step matr way n curr =
  83.     if (length way) == (Matrix.ncols matr) then
  84.         -- trace ("way: " ++ show way ++ "cur: " ++ show curr) $
  85.         Just (way, curr)
  86.     else
  87.         foldl
  88.             folding
  89.             Nothing
  90.             (
  91.                 map
  92.                 (\ (ind, val) ->
  93.                     solve_step
  94.                     matr
  95.                     (way ++ [ind])
  96.                     ind
  97.                     (curr + val)
  98.                 )
  99.                 (
  100.                     map
  101.                     (\ (ind , Just val) ->
  102.                         (ind, val)
  103.                     )
  104.                     (
  105.                         filter
  106.                         (\
  107.                             (ind, val) ->
  108.                                 if (length way) == ((Matrix.nrows matr) - 1) then
  109.                                     (ind == 1) && (Maybe.isJust val)
  110.                                 else
  111.                                     (not $ elem ind way) && (not $ ind == 1) && (Maybe.isJust val)
  112.                           )
  113.                         (enumerate row)
  114.                     )
  115.                 )
  116.             )
  117.         where row = Vector.toList (Matrix.getRow n matr)
  118.  
  119. solve matr = solve_step matr [] 1 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement