結果

問題 No.157 2つの空洞
ユーザー ducktailducktail
提出日時 2018-07-26 17:24:11
言語 Haskell
(9.8.2)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,043 bytes
コンパイル時間 5,766 ms
コンパイル使用メモリ 197,156 KB
実行使用メモリ 7,968 KB
最終ジャッジ日時 2023-09-13 18:41:55
合計ジャッジ時間 7,074 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,016 KB
testcase_01 AC 2 ms
7,004 KB
testcase_02 AC 2 ms
7,108 KB
testcase_03 AC 2 ms
7,068 KB
testcase_04 AC 2 ms
7,092 KB
testcase_05 AC 2 ms
7,148 KB
testcase_06 AC 2 ms
7,160 KB
testcase_07 AC 2 ms
7,152 KB
testcase_08 AC 2 ms
7,068 KB
testcase_09 AC 3 ms
7,128 KB
testcase_10 AC 2 ms
7,024 KB
testcase_11 AC 3 ms
7,296 KB
testcase_12 AC 2 ms
7,164 KB
testcase_13 AC 2 ms
7,144 KB
testcase_14 AC 2 ms
7,440 KB
testcase_15 AC 3 ms
7,548 KB
testcase_16 AC 2 ms
7,436 KB
testcase_17 AC 3 ms
7,968 KB
testcase_18 AC 2 ms
7,300 KB
testcase_19 AC 2 ms
7,144 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.6.1/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.Vector.Unboxed (Vector, (!), (//))
import qualified Data.Vector.Unboxed as V

main :: IO ()
main = do
  [w, h] <- map read <$> words <$> getLine
  solve w h <$> (concat <$> replicateM h getLine) >>= print

solve :: Int -> Int -> String -> Int
solve w h ss = let imp = V.fromList ss
                   (mp, ixs1) = dfs [maybe (0,0) fmIx $ V.findIndex (=='.') imp] imp []
                   (_, ixs2) = dfs [maybe (0,0) fmIx $ V.findIndex (=='.') mp] mp []
               in minPath ixs1 ixs2
  where toIx (i, j) = i * w + j
        fmIx ix = divMod ix w
        dfs [] mp ixs = (mp, ixs)
        dfs (p:ps) mp ixs | mp ! (toIx p) == '#' = dfs ps mp ixs
                          | otherwise = let (i, j) = p
                                        in dfs ([(i+1,j),(i-1,j),(i,j+1),(i,j-1)] ++ ps) (mp // [(toIx p, '#')]) (p:ixs)
        minPath xs ys = minimum $ do
          (i,j) <- xs
          (i',j') <- ys
          return $ abs (i - i') + abs (j - j') - 1
0