結果

問題 No.55 正方形を描くだけの簡単なお仕事です。
ユーザー くれちーくれちー
提出日時 2017-02-13 23:08:54
言語 Haskell
(9.8.2)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,023 bytes
コンパイル時間 6,828 ms
コンパイル使用メモリ 176,512 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-09 10:52:59
合計ジャッジ時間 7,548 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 1 ms
6,944 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 1 ms
6,944 KB
testcase_24 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.8.2/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )

Main.hs:49:58: warning: [GHC-63394] [-Wx-partial]
    In the use of ‘head’
    (imported from Prelude, but defined in GHC.List):
    "This is a partial function, it throws an error on empty lists. Use pattern matching or Data.List.uncons instead. Consider refactoring to use Data.List.NonEmpty."
   |
49 |         point       = (\(Vector p) -> p) <$> ((vPlus <$> head <*> last) <$> find f2 vectors)
   |                                                          ^^^^
[2 of 2] Linking a.out

ソースコード

diff #

import Control.Monad (replicateM)
import Control.Applicative ((<$>), (<*>))
import Data.List (nub, find, findIndex)

data Point a = Point a a
    deriving (Eq)

instance Show a => Show (Point a) where
    show (Point x y) = show x ++ " " ++ show y

data Vector a = Vector (Point a)
    deriving (Eq)

movePoint :: Integral a => Point a -> Vector a -> Point a
movePoint (Point x1 y1) (Vector (Point x2 y2)) = Point (x1 + x2) (y1 + y2)

makeVectorP2P :: Integral a => Point a -> Point a -> Vector a
makeVectorP2P (Point x1 y1) (Point x2 y2) = Vector (Point (x2 - x1) (y2 - y1))

vSize :: (Integral a, Floating b) => Vector a -> b
vSize (Vector (Point x y)) = sqrt $ fromIntegral (x ^ 2 + y ^ 2)

vPlus :: Integral a => Vector a -> Vector a -> Vector a
vPlus (Vector (Point x1 y1)) (Vector (Point x2 y2)) = Vector (Point (x1 + x2) (y1 + y2))

dotProd :: Integral a => Vector a -> Vector a -> a
dotProd (Vector (Point x1 y1)) (Vector (Point x2 y2)) = x1 * x2 + y1 * y2

isVertical ::  Integral a => Vector a -> Vector a -> Bool
isVertical a b = dotProd a b == 0

genPerm :: Eq a => Int -> [a] -> [[a]]
genPerm n l = filter (\x -> nub x == x) $ replicateM n l

splitAt' :: Int -> [a] -> [[a]]
splitAt' n xs
    | length xs <= n = [xs]
    | otherwise      = [fst xs'] ++ (splitAt' n $ snd xs')
    where
        xs' = splitAt n xs

solve :: Integral a => Point a -> Point a -> Point a -> Maybe (Point a)
solve p1 p2 p3 = ans
    where
        f1 [p1, p2] = makeVectorP2P p1 p2
        f2 [v1, v2] = isVertical v1 v2 && vSize v1 == vSize v2
        vectors     = splitAt' 2 $ map f1 $ genPerm 2 [p1, p2, p3]
        origin      = (!!) <$> pure [p1, p2, p3] <*> findIndex f2 vectors
        point       = (\(Vector p) -> p) <$> ((vPlus <$> head <*> last) <$> find f2 vectors)
        ans         = movePoint <$> point <*> (Vector <$> origin)

main = do
    [x1, y1, x2, y2, x3, y3] <- map read . words <$> getLine
    case solve (Point x1 y1) (Point x2 y2) (Point x3 y3) of
        Just p  -> print p
        Nothing -> print (-1)
0