結果

問題 No.55 正方形を描くだけの簡単なお仕事です。
ユーザー くれちーくれちー
提出日時 2017-02-13 23:08:54
言語 Haskell
(9.8.2)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 2,023 bytes
コンパイル時間 3,037 ms
コンパイル使用メモリ 178,052 KB
実行使用メモリ 7,216 KB
最終ジャッジ日時 2023-08-28 15:42:22
合計ジャッジ時間 4,549 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,064 KB
testcase_01 AC 2 ms
7,064 KB
testcase_02 AC 2 ms
7,076 KB
testcase_03 AC 2 ms
7,080 KB
testcase_04 AC 3 ms
7,120 KB
testcase_05 AC 2 ms
7,152 KB
testcase_06 AC 2 ms
7,104 KB
testcase_07 AC 2 ms
7,080 KB
testcase_08 AC 3 ms
7,152 KB
testcase_09 AC 2 ms
7,088 KB
testcase_10 AC 3 ms
7,100 KB
testcase_11 AC 3 ms
7,172 KB
testcase_12 AC 3 ms
7,156 KB
testcase_13 AC 2 ms
7,216 KB
testcase_14 AC 3 ms
7,100 KB
testcase_15 AC 2 ms
7,180 KB
testcase_16 AC 2 ms
7,084 KB
testcase_17 AC 2 ms
7,092 KB
testcase_18 AC 3 ms
7,196 KB
testcase_19 AC 3 ms
7,104 KB
testcase_20 AC 2 ms
7,204 KB
testcase_21 AC 2 ms
7,112 KB
testcase_22 AC 2 ms
7,092 KB
testcase_23 AC 3 ms
7,152 KB
testcase_24 AC 3 ms
7,140 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.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