(Haskell Language). How to separate and label clusters ( 4 direction connected c
ID: 3808667 • Letter: #
Question
(Haskell Language).
How to separate and label clusters ( 4 direction connected components) in a 2D array? ( Haskell code will be a must :(
For example, I have a list of list number in haskell like this:
["122221111211",
"211121221121",
"221211222211",
"111212122112",
"112121212222",
"112222121211",
"211211221112"]
And I want to label the connected components (4 directions)to be like this:
"1 2 2 2 2 3 3 3 3 4 5 5"
"6 7 7 7 2 3 8 8 3 3 9 5"
"6 6 7 10 3 3 8 8 8 8 5 5"
"7 7 7 10 3 11 12 8 8 5 5 8"
"7 7 13 14 13 15 16 17 8 8 8 8"
"7 7 13 13 13 13 18 19 20 8 20 20"
"21 7 7 13 22 22 19 19 20 20 20 23"
the color version:
the way to label the clusters is like the flood fill, if it connects (same number) to their neighbor by up-down-left-right direction, they will be grouping in the same cluster. we go from top left to bottom right. Please help me solve this problem. Thanks!!!
(I have to write this by Haskell)
Explanation / Answer
getRow :: [(Integer, Integer, Integer)] -> Int -> (Integer, Integer, Integer) getRow matrix row = matrix !! (row :: Int) getElem :: [(Integer, Integer, Integer)] -> Int -> Int -> Integer getElem matrix row column | column == 0 = x | column == 1 = y | column == 2 = z where (x, y, z) = getRow matrix row
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.