@@ -392,7 +392,6 @@ genPerm' = genPermHelper [[]]
392392 [Yellow,Yellow,Blue]
393393 ]
394394-}
395-
396395data Color = Red | Yellow | Blue deriving Show
397396
398397instance Eq Color where
@@ -410,6 +409,7 @@ data Direction = Up | Down | PLeft | PRight
410409
411410type Image = V. Vector (V. Vector Color )
412411
412+ -- test case
413413image :: Image
414414image = V. fromList
415415 [
@@ -418,6 +418,7 @@ image = V.fromList
418418 V. fromList [Yellow , Yellow , Blue ]
419419 ]
420420
421+ -- fill up a color
421422fillUpColor :: Image -> (Int , Int ) -> Color -> Image
422423fillUpColor img (i,j) c = foldl (\ acc x -> paint acc x c ) img pList
423424 where pList = findArea img (i,j)
@@ -442,12 +443,14 @@ findArea img (i,j) = uniq (
442443 findAreaOnDir img (i,j) boundC PRight ) []
443444 where boundC = img V. ! i V. ! j
444445
446+ -- remove duplicates
445447uniq :: [(Int , Int )] -> [(Int , Int )]-> [(Int , Int )]
446448uniq [] buf = buf
447449uniq (x: xs) buf
448450 | x `elem` buf = uniq xs buf
449451 | otherwise = uniq xs (x: buf)
450452
453+ -- find potential position by direction
451454findAreaOnDir :: Image -> (Int , Int ) -> Color -> Direction -> [(Int , Int )]
452455findAreaOnDir img (i,j) c Up
453456 | isInBoundAndSameColor img (i,j- 1 ) c =
@@ -482,10 +485,12 @@ findAreaOnDir img (i,j) c PRight
482485 (i+ 1 ,j): findAreaOnDir img (i+ 1 ,j) c Down
483486 | otherwise = []
484487
488+ -- condition determine potential fill up position
485489isInBoundAndSameColor :: Image -> (Int , Int ) -> Color -> Bool
486490isInBoundAndSameColor img (i,j) c = isInBound img (i,j) && selectC == c
487491 where selectC = img V. ! i V. ! j
488492
493+ -- check if position if in bound
489494isInBound :: Image -> (Int , Int ) -> Bool
490495isInBound img (i,j)
491496 | (0 <= i && i < xBound) && (0 <= j && j < yBound) = True
0 commit comments