I recently got an assignment, where I a have 4 by 5 matrix with 0 and 1 (zero's represent the cut parts of the matrix). I need to find a way to calculate in how many pieces matrix will be sliced and as I mentioned zero's represent the cut parts of the matrix so if from one border to another goes straight line of zero's it means the matrix was sliced on that line, as for example (in this pic I've marked where matrix would be split, a line of zeros slice it):
So guys I know that you won't entirely solve this code for me and I don't need that, but what I need is to understand this:
Any help would be appreciated
You did not specify any requirements for the algorithm (such as time and space complexity), so I guess one answer, that correlates to some specific solutions, would be:
A general algorithm for this can be implemented as follows:
Whenever you find 1, try to store the values in the helper matrix. If there is already a value there, then:
Store in the collisions data structure that you found a collision between 2 symbols
Don't continue in any direction from this one.
This will traverse each cell at most 4 time, so time complexity is O(n)
, and when you are done you will have a data structure with all the collisions.
Now all you need to do is combine all entries in the other data structure to collect how many unique pieces you really have.