結果
| 問題 |
No.55 正方形を描くだけの簡単なお仕事です。
|
| コンテスト | |
| ユーザー |
くれちー
|
| 提出日時 | 2017-02-13 23:08:54 |
| 言語 | Haskell (9.10.1) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 2,023 bytes |
| コンパイル時間 | 12,278 ms |
| コンパイル使用メモリ | 175,872 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-12-29 19:11:28 |
| 合計ジャッジ時間 | 13,182 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 21 |
コンパイルメッセージ
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
ソースコード
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)
くれちー