結果

問題 No.707 書道
コンテスト
ユーザー momen999
提出日時 2019-03-13 19:10:40
言語 Haskell
(9.14.1)
コンパイル:
ghc -rtsopts -with-rtsopts=-K1G -o a.out -O2 _filename_
実行:
./a.out
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 1,071 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,361 ms
コンパイル使用メモリ 194,560 KB
実行使用メモリ 9,984 KB
最終ジャッジ日時 2026-03-09 04:37:03
合計ジャッジ時間 2,994 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 6
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.14.1/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
Main.hs:33:13: warning: [GHC-63394] [-Wx-partial]
    In the use of ‘head’
    (imported from Prelude, but defined in GHC.Internal.List):
    "This is a partial function, it throws an error on empty lists. Use pattern matching, 'Data.List.uncons' or 'Data.Maybe.listToMaybe' instead. Consider refactoring to use "Data.List.NonEmpty"."
   |
33 |     print $ head $ sort $ solve (getWaku w h) (getMasu 0 w h ps)
   |             ^^^^

[2 of 2] Linking a.out

ソースコード

diff #
raw source code

import Control.Monad (replicateM)
import Data.List (sort)

distance :: (Int, Int) -> (Int, Int) -> Double
distance a b = sqrt $ distance2 a b

distance2 :: (Int, Int) -> (Int, Int) -> Double
distance2 a b = x2 + y2 where
    x2 = (fromIntegral (fst a) - fromIntegral (fst b)) ** 2
    y2 = (fromIntegral (snd a) - fromIntegral (snd b)) ** 2

solve :: [(Int, Int)] -> [(Int, Int)] -> [Double]
solve [] _ = []
solve (w:ws) ms = d : solve ws ms where
    d = foldr (\x y -> distance w x + y) 0.0 ms

getWaku w h = [ (x, 0) | x <- [1..w] ] ++
              [ (0, y) | y <- [1..h] ] ++
              [ (x, h + 1) | x <- [1..w] ] ++
              [ (w + 1, y) | y <- [1..h] ]

getMasu _ _ _ [] = []
getMasu i w h (c:ps)
    | c == '1'      = masu : getMasu (i + 1) w h ps
    | otherwise     = getMasu (i + 1) w h ps
    where
        (d, m) = divMod i w
        masu = (m + 1, d + 1)

main = do
    [h, w] <- map read . words <$> getLine :: IO [Int]
    ps <- foldr (++) [] <$> replicateM h getLine :: IO String
    print $ head $ sort $ solve (getWaku w h) (getMasu 0 w h ps)
0