I am given an arbitrary binary matrix of the form:
Input = [
[0,0,0,0,0],
[0,0,1,1,0],
[0,1,1,1,0],
[1,0,0,0,1]
]
My objective is to filter this binary matrix so it only contains "islands" of size n or bigger. I am only searching horizontally and not wrapping around matrix edges. If for example, I wanted islands of size 2 or bigger, the output would be:
Output = [
[0,0,0,0,0],
[0,0,1,1,0],
[0,1,1,1,0],
[0,0,0,0,0]
]
I am having trouble figuring out how to set this problem up. I know I need to do to a nested for loop to search through but I don't know how to generalize this problem so it works for any size island or any arbitrary matrix. The first thought that came to mind was DFS but I am not too sure how to implement DFS with a minimum island condition. Also I am only searching horizontally, not vertically and not wrapping around matrix edges. Any thoughts or tips on this problem would be greatly appreciated.
question from:
https://stackoverflow.com/questions/65602438/filtering-a-binary-matrix-to-only-contain-groups-that-meet-a-minimum-size 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…