import Control.Applicative
import qualified Data.List as L
replace
:: Eq a
=> a
-> a
-> [a
] -> [a
] where f x | x == from = to
blank = '□'
insertWord word i j = xs : [L.transpose xs]
where
xs
= replicate j
(replicate
(length word
+ i
) blank
) ++ [replicate i blank
++ word
]
blankMatrix (i, j) = [[blank | _ <- [1..j]] | _ <- [1..i]]
infixr 2 .||
xss1
.|| yss1
= maybe [] id $ f xss1 yss1
where
f (xs:xss) (ys:yss) = (:) <$> g xs ys <*> f xss yss
f (xs:xss) _ = (:) <$> g xs [] <*> f xss []
f _ (ys:yss) = (:) <$> g [] ys <*> f [] yss
f _ _ = Just []
g (x:xs) (y:ys) | x == blank = (y:) <$> g xs ys
| y == blank = (x:) <$> g xs ys
| x == y = (x:) <$> g xs ys
g (x:xs) _ = (x:) <$> g xs []
g _ (y:ys) = (y:) <$> g [] ys
g _ _ = Just []
listup xs = foldM f [] xs
where
f a x
= (a
.||) <$> concat [insertWord x i j
| i
<- [0..len
], j
<- [0..len
]]
isCrossWord wordList matrix = L.sort wordList == L.sort pzWords && checkLen
where
xst = L.transpose matrix
sumc xs
= foldr (\x a
-> if x
== blank
then a
else a
+ 1) 0 xs
findCrossWord wordList =
liftA2
(.||) id (blankMatrix
. getWH
) <$> L
.find
(isCrossWord wordList
) (listup wordList
)
main = do
forM_ [ ["スイカ", "カイバ", "イヌ"], ["マンガ", "バター"] ] $
aW1wb3J0IENvbnRyb2wuQXBwbGljYXRpdmUKaW1wb3J0IENvbnRyb2wuTW9uYWQKaW1wb3J0IHF1YWxpZmllZCBEYXRhLkxpc3QgYXMgTAoKcmVwbGFjZSA6OiBFcSBhID0+IGEgLT4gYSAtPiBbYV0gLT4gW2FdCnJlcGxhY2UgZnJvbSB0byA9IG1hcCBmCiAgICB3aGVyZSBmIHggfCB4ID09IGZyb20gPSB0bwogICAgICAgICAgICAgIHwgb3RoZXJ3aXNlID0geAoKYmxhbmsgOjogQ2hhcgpibGFuayA9ICfilqEnCgppbnNlcnRXb3JkIDo6IFN0cmluZyAtPiBJbnQgLT4gSW50IC0+IFtbU3RyaW5nXV0KaW5zZXJ0V29yZCB3b3JkIGkgaiA9IHhzIDogW0wudHJhbnNwb3NlIHhzXQogICAgd2hlcmUKICAgICAgeHMgPSByZXBsaWNhdGUgaiAocmVwbGljYXRlIChsZW5ndGggd29yZCArIGkpIGJsYW5rKSArKyBbcmVwbGljYXRlIGkgYmxhbmsgKysgd29yZF0KCmJsYW5rTWF0cml4IDo6IChJbnQsIEludCkgLT4gW1N0cmluZ10KYmxhbmtNYXRyaXggKGksIGopID0gW1tibGFuayB8IF8gPC0gWzEuLmpdXSB8IF8gPC0gWzEuLmldXQoKaW5maXhyIDIgLnx8CigufHwpIDo6IFtTdHJpbmddIC0+IFtTdHJpbmddIC0+IFtTdHJpbmddCnhzczEgLnx8IHlzczEgPSBtYXliZSBbXSBpZCAkIGYgeHNzMSB5c3MxCiAgICB3aGVyZQogICAgICBmIDo6IFtTdHJpbmddIC0+IFtTdHJpbmddIC0+IE1heWJlIFtTdHJpbmddCiAgICAgIGYgKHhzOnhzcykgKHlzOnlzcykgPSAoOikgPCQ+IGcgeHMgeXMgPCo+IGYgeHNzIHlzcwogICAgICBmICh4czp4c3MpIF8gICAgICAgID0gKDopIDwkPiBnIHhzIFtdIDwqPiBmIHhzcyBbXQogICAgICBmIF8gICAgICAgICh5czp5c3MpID0gKDopIDwkPiBnIFtdIHlzIDwqPiBmIFtdIHlzcwogICAgICBmIF8gICAgICAgICBfICAgICAgID0gSnVzdCBbXQogICAgICBnIDo6IFN0cmluZyAtPiBTdHJpbmcgLT4gTWF5YmUgU3RyaW5nCiAgICAgIGcgKHg6eHMpICh5OnlzKSB8IHggPT0gYmxhbmsgPSAoeTopIDwkPiBnIHhzIHlzCiAgICAgICAgICAgICAgICAgICAgICB8IHkgPT0gYmxhbmsgPSAoeDopIDwkPiBnIHhzIHlzCiAgICAgICAgICAgICAgICAgICAgICB8IHggPT0geSAgICAgPSAoeDopIDwkPiBnIHhzIHlzC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