Submission #1611165


Source Code Expand

{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}

import Debug.Trace
import Data.Maybe
import qualified Data.Set as S
import qualified Data.List as L
import qualified Data.IntMap as M
import qualified Data.Sequence as Q
import qualified Data.ByteString.Char8 as B

import qualified Data.Foldable as F

import Data.Array
import Data.Array.ST
import Control.Monad.ST

type Index = Int
type StartPoints = S.Set (Index, Index)
type Board = (M.IntMap B.ByteString)
type Memo = (M.IntMap (M.IntMap Int))
type Point = (Index,Index)

type Visited s = (ST s) (STUArray s (Int,Int) Int)

d = [(-1, 0), (1, 0), (0, -1), (0, 1)]

make :: Int -> Int -> Visited s
make h w = newArray ((0,0), (h-1,w-1)) 0

dfs :: Board -> Int -> Point -> Int -> Int -> StartPoints
dfs b k (sy,sx) h w = runST $
  make h w
  >>= \visited ->
        dfs' b k (sy,sx) (visited, S.fromList [(sy,sx)]) h w
        >>= \(visited,l) -> return l

dfs' b k p@(y,x) (visited,l) h w =
  if k == 0 then return (visited,l)
  else F.foldlM (\z d -> step (z,p) d) (visited,l) d
  where
    ng y x = y < 0 || y >= h || x < 0 || x >= w
    step ((visited,l),p@(y,x)) (dy,dx) =
      let y' = y + dy
          x' = x + dx
          p' = (y',x')
          k' = k - 1
      in if ng y' x'
      then return (visited,l)
      else readArray visited p'
           >>= \u -> if k' < u || v b y' x' /= '.'
                     then return (visited,l)
                     else writeArray visited p' k' >>
                          dfs' b k' p' (visited, S.insert p' l) h w


listStart :: Board -> Int -> Int -> Int -> StartPoints
listStart b k = dfs b k s
  where
    s = find b 'S'

solve :: [B.ByteString] -> Int
solve (xs:xss) = 1 + ceiling (fromIntegral m / fromIntegral k)
  where
    [h,w,k] = map readInt $ B.words xs
    b = M.fromList $ zip [0..] xss
    s = listStart b k h w
    m = S.foldl' (\z (y,x) -> min z (minimum [y, x, h-y-1, w-x-1])) 1000000000 s

readInt = fst . fromJust . B.readInt
main = B.getContents >>= print . solve .  B.lines

--------------------------------------------------
-- Matrix
--------------------------------------------------
class (Show a) => Matrix a b | a -> b, b -> a where
   v :: a -> Int -> Int -> b
   find :: a -> b -> (Int,Int)
   findAll :: a -> b -> [(Int,Int)]
   update :: a -> Int -> Int -> b -> a
   initMat :: Int -> Int -> b -> a

instance Matrix Memo Int where
  v b i j = (b M.! i) M.! j
  update b i j c = M.update (Just . M.update (\x -> Just c) j) i b
  initMat i j c = M.fromList $ zip [0..(i-1)] $ L.replicate i xs
    where xs = M.fromList $ zip [0..(j-1)] $ L.replicate j c
  findAll b c = concatMap (\(y,xs) -> zip [y,y..] xs) $ M.toList $ M.map (map fst . M.toList . M.filter (== c)) b

instance Matrix Board Char where
  v b i j = B.index (b M.! i) j
  find b c = head tmp3
    where
      tmp = M.map (B.elemIndex c) b
      tmp2 = M.filterWithKey (\i x -> isJust x) tmp
      tmp3 = map (\(i, x) -> (i, fromJust x)) (M.toList tmp2)
  update b i j c = M.update (Just . replace) i b
    where
      replace x = B.append (B.take j x) $ B.cons c (B.drop (j+1) x)

pp b d = M.foldl' (\z' xs -> z' ++ M.foldl' (\z y -> z ++ (if y == d then "x" else show y)) "" xs ++ " ") "" b

Submission Info

Submission Time
Task C - Closed Rooms
User waaadaaap
Language Haskell (GHC 7.10.3)
Score 0
Code Size 3431 Byte
Status TLE
Exec Time 2149 ms
Memory 685436 KB

Compile Error

Main.hs:86:10: Warning:
    No explicit implementation for
      ‘find’
    In the instance declaration for ‘Matrix Memo Int’

Main.hs:93:10: Warning:
    No explicit implementation for
      ‘findAll’ and ‘initMat’
    In the instance declaration for ‘Matrix Board Char’

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 700
Status
AC × 3
AC × 39
TLE × 7
Set Name Test Cases
Sample sample1.txt, sample2.txt, sample3.txt
All sample1.txt, sample2.txt, sample3.txt, in1.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in2.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in28.txt, in29.txt, in3.txt, in30.txt, in31.txt, in32.txt, in33.txt, in34.txt, in35.txt, in36.txt, in37.txt, in38.txt, in39.txt, in4.txt, in40.txt, in5.txt, in6.txt, in7.txt, in8.txt, in9.txt, sample1.txt, sample2.txt, sample3.txt
Case Name Status Exec Time Memory
in1.txt AC 4 ms 7292 KB
in10.txt AC 5 ms 7420 KB
in11.txt AC 4 ms 7292 KB
in12.txt AC 4 ms 7292 KB
in13.txt AC 4 ms 7292 KB
in14.txt AC 4 ms 7292 KB
in15.txt AC 4 ms 7292 KB
in16.txt AC 4 ms 7292 KB
in17.txt AC 35 ms 13692 KB
in18.txt AC 4 ms 7292 KB
in19.txt AC 6 ms 7292 KB
in2.txt AC 4 ms 7292 KB
in20.txt AC 2 ms 892 KB
in21.txt AC 1 ms 508 KB
in22.txt AC 1 ms 508 KB
in23.txt AC 4 ms 7164 KB
in24.txt AC 4 ms 7292 KB
in25.txt AC 4 ms 7292 KB
in26.txt TLE 2134 ms 490620 KB
in27.txt AC 4 ms 7292 KB
in28.txt TLE 2140 ms 568572 KB
in29.txt TLE 2127 ms 367868 KB
in3.txt AC 4 ms 7292 KB
in30.txt TLE 2126 ms 362748 KB
in31.txt AC 4 ms 7164 KB
in32.txt AC 4 ms 7292 KB
in33.txt TLE 2143 ms 627452 KB
in34.txt TLE 2138 ms 558716 KB
in35.txt TLE 2149 ms 685436 KB
in36.txt AC 4 ms 7292 KB
in37.txt AC 4 ms 7292 KB
in38.txt AC 4 ms 7292 KB
in39.txt AC 4 ms 7292 KB
in4.txt AC 4 ms 7292 KB
in40.txt AC 4 ms 7292 KB
in5.txt AC 4 ms 7292 KB
in6.txt AC 4 ms 7292 KB
in7.txt AC 4 ms 7292 KB
in8.txt AC 4 ms 7164 KB
in9.txt AC 4 ms 7292 KB
sample1.txt AC 1 ms 380 KB
sample2.txt AC 1 ms 380 KB
sample3.txt AC 1 ms 380 KB