Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
100
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. consoleInput = do
  18.     input <- getLine
  19.     inputs <- Monad.replicateM (read input :: Int) getLine
  20.     map (\(str) -> read str ::[Int]) inputs
  21.  
  22.  
  23. matrixShift :: Matrix.Matrix Int -> Matrix.Matrix (Maybe Int)
  24. matrixShift mat =
  25.     Matrix.matrix
  26.     (Matrix.nrows mat)
  27.     (Matrix.ncols mat)
  28.     (\(i,j) ->
  29.         let elem = Matrix.getElem i j mat
  30.         in
  31.         if elem == 0 then
  32.             Nothing
  33.         else
  34.             Just elem
  35.     )
  36.  
  37. -- matrRead :: [[ Int ]]
  38. -- matrRead = read
  39.  
  40. main = do
  41.     let inp = Matrix.fromLists consoleInput
  42.     print inp
  43.     print $ solve (matrixShift inp)
  44.  
  45.  
  46. folding :: Maybe([Int], Int) -> Maybe([Int], Int) -> Maybe([Int], Int)
  47. folding (Just sum) (Just curr) =
  48.     if snd(sum) < snd(curr) then
  49.         Just sum
  50.     else
  51.         Just curr
  52.  
  53. folding Nothing (Just curr) =
  54.     Just curr
  55.  
  56. folding Nothing Nothing =
  57.     Nothing
  58.  
  59. folding (Just sum) Nothing =
  60.     Just sum
  61.  
  62. enumerate :: [Maybe Int] -> [(Int, Maybe Int)]
  63. enumerate row =
  64.     map (\(x, y) -> ((fromInteger x), y)) (zip [1..] row)
  65.  
  66. solve_step :: Matrix.Matrix(Maybe Int) -> [Int] -> Int -> Int -> Maybe ([Int], Int)
  67. solve_step matr way n curr =
  68.     if (length way) == (Matrix.ncols matr) then
  69.         -- trace ("way: " ++ show way ++ "cur: " ++ show curr) $
  70.         Just (way, curr)
  71.     else
  72.         foldl
  73.             folding
  74.             Nothing
  75.             (
  76.                 map
  77.                 (\ (ind, val) ->
  78.                     solve_step
  79.                     matr
  80.                     (way ++ [ind])
  81.                     ind
  82.                     (curr + val)
  83.                 )
  84.                 (
  85.                     map
  86.                     (\ (ind , Just val) ->
  87.                         (ind, val)
  88.                     )
  89.                     (
  90.                         filter
  91.                         (\
  92.                             (ind, val) ->
  93.                                 if (length way) == ((Matrix.nrows matr) - 1) then
  94.                                     (ind == 1) && (Maybe.isJust val)
  95.                                 else
  96.                                     (not $ elem ind way) && (not $ ind == 1) && (Maybe.isJust val)
  97.                           )
  98.                         (enumerate row)
  99.                     )
  100.                 )
  101.             )
  102.         where row = Vector.toList (Matrix.getRow n matr)
  103.  
  104. solve matr = solve_step matr [] 1 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement