日本情報オリンピック 第5回本選 第5問

まだ途中。富豪的な発想で。やっぱりこれじゃあダメだよねぇ。

module Main (main) where

import Data.List
import System

type Points = [[Int]]

point :: (Int,Int) -> (Int,Int) -> Points
point (p,q) (r,s) = sequence $ [[p..(r-1)],[q..(s-1)]]

makePoints :: String -> Points
makePoints l = let ns = map (read) $ words l
               in point (ns !! 0, ns !! 1) (ns !! 2, ns !! 3)

addPoints :: Points -> Points -> Points
addPoints x y = nub $ x ++ y

pointsArea = length

pointsLength ps = (-) (4 * (length ps)) (length dups)
    where
        dups = [x| p <- ps, x <- intersect (neighbor p) ps]
        neighbor (x,y) = [(x-1,y),(x+1,y),(x,y-1),(x,y+1)]

listToTuple (x:y:_) = (x,y)

main :: IO ()
main = do
    (inputFile:_) <- getArgs
    contents <- readFile inputFile
    let
        ls = lines contents
        (n:t:_) = map read $ words $ head ls
        points = foldr1 addPoints $ map (makePoints) $ take n $ tail ls
        area = pointsArea points
        len  = pointsLength $ map (listToTuple) points
    if t == 1
        then writeFile "output.txt" $ show area
        else writeFile "output.txt" $ unlines [show area, show len]