@@ -392,7 +392,6 @@ genPerm' = genPermHelper [[]]
392
392
[Yellow,Yellow,Blue]
393
393
]
394
394
-}
395
-
396
395
data Color = Red | Yellow | Blue deriving Show
397
396
398
397
instance Eq Color where
@@ -410,6 +409,7 @@ data Direction = Up | Down | PLeft | PRight
410
409
411
410
type Image = V. Vector (V. Vector Color )
412
411
412
+ -- test case
413
413
image :: Image
414
414
image = V. fromList
415
415
[
@@ -418,6 +418,7 @@ image = V.fromList
418
418
V. fromList [Yellow , Yellow , Blue ]
419
419
]
420
420
421
+ -- fill up a color
421
422
fillUpColor :: Image -> (Int , Int ) -> Color -> Image
422
423
fillUpColor img (i,j) c = foldl (\ acc x -> paint acc x c ) img pList
423
424
where pList = findArea img (i,j)
@@ -442,12 +443,14 @@ findArea img (i,j) = uniq (
442
443
findAreaOnDir img (i,j) boundC PRight ) []
443
444
where boundC = img V. ! i V. ! j
444
445
446
+ -- remove duplicates
445
447
uniq :: [(Int , Int )] -> [(Int , Int )]-> [(Int , Int )]
446
448
uniq [] buf = buf
447
449
uniq (x: xs) buf
448
450
| x `elem` buf = uniq xs buf
449
451
| otherwise = uniq xs (x: buf)
450
452
453
+ -- find potential position by direction
451
454
findAreaOnDir :: Image -> (Int , Int ) -> Color -> Direction -> [(Int , Int )]
452
455
findAreaOnDir img (i,j) c Up
453
456
| isInBoundAndSameColor img (i,j- 1 ) c =
@@ -482,10 +485,12 @@ findAreaOnDir img (i,j) c PRight
482
485
(i+ 1 ,j): findAreaOnDir img (i+ 1 ,j) c Down
483
486
| otherwise = []
484
487
488
+ -- condition determine potential fill up position
485
489
isInBoundAndSameColor :: Image -> (Int , Int ) -> Color -> Bool
486
490
isInBoundAndSameColor img (i,j) c = isInBound img (i,j) && selectC == c
487
491
where selectC = img V. ! i V. ! j
488
492
493
+ -- check if position if in bound
489
494
isInBound :: Image -> (Int , Int ) -> Bool
490
495
isInBound img (i,j)
491
496
| (0 <= i && i < xBound) && (0 <= j && j < yBound) = True
0 commit comments