Created
May 23, 2013 15:52
-
-
Save banacorn/5637107 to your computer and use it in GitHub Desktop.
生測資
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Monad (mapM_) | |
import System.Random | |
import System.Cmd | |
import System.Environment (getArgs) | |
import Data.List (intercalate, nub) | |
type Size = Int | |
type Constraint = (Variable, Variable) | |
data Variable = Variable Int deriving (Eq) | |
data Map = Map Size Size [Constraint] | |
instance Show Variable where | |
show (Variable n) = "V" ++ show n | |
instance Show Map where | |
show (Map v c p) = "4 " ++ show v ++ " " ++ show c ++ "\n" ++ variables ++ "\n" ++ pairs | |
where variables = intercalate " " $ map (show . Variable) [0 .. pred v] | |
pairs = intercalate "\n" $ map (\(a, b) -> show a ++ " " ++ show b) p | |
color = 4 | |
constraints :: Size -> Int -> [Constraint] | |
constraints numberOfVariables seed = nub . take numberOfConstraints . map pickConstraint $ randomRs (0, upperBound - 1) gen' | |
where gen = mkStdGen seed | |
(numberOfConstraints, gen') = randomR (0, upperBound) gen | |
upperBound = (numberOfVariables - 1) * numberOfVariables `div` 2 | |
pickConstraint = (!!) (constraintPairs numberOfVariables) | |
constraintPairs n = [ (Variable a, Variable b) | a <- [0..pred n], b <- [0..pred n], a < b] | |
makeMap seed n = Map n (length pairs) pairs | |
where pairs = constraints n seed | |
genTest n = return $ map (makeMap 40) [0..n] | |
writeMap m = writeFile ("test" ++ show size ++ ".txt") (show m) | |
where (Map size _ _) = m | |
main = do | |
fmap (read . head) getArgs >>= genTest >>= mapM_ writeMap |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment