Class MatrixGroupsRemover<T>
java.lang.Object
org.jjazz.utilities.api.MatrixGroupsRemover<T>
- Type Parameters:
T- The type of the values in the matrix (e.g., Character, Integer, etc.).
Determine the minimum sequence of "group" removals needed to clear a matrix.
A "group" is a set of cells that share the same non-null value and can span multiple rows or columns (adjacent or not) with these alignment rules:
- If a group spans multiple rows, it must occupy the same columns in each row.
- If a group spans multiple columns, it must occupy the same rows in each column.
Input matrix example:
A B A A A A B B B Groups of min 2 cells: (row, col) 1. (0,0), (0,2), (1,0), (1,2) - 'A' 2. (1,0), (1,1), (1,2) - 'A' the middle line 3. (2,0), (2,1), (2,2) - 'B' the last line 4. (0,1), (2,1) - 'B'
This class uses a greedy algorithm to find the minimum number of removals required to clear the matrix.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordRepresents the coordinates of a single cell in the matrix.static final recordRepresents a group of cells with the same value. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionFinds the minimum sequence of groups needed to remove all cells in the matrix.static <T> voidprintMatrix(T[][] matrix) Utility method to pretty-print the input matrix.static <T> voidprintRemovalSequence(List<MatrixGroupsRemover.Group<T>> removalSequence) Utility method to pretty-print the result of findRemovalSequence().
-
Constructor Details
-
MatrixGroupsRemover
- Parameters:
grid- The matrix containing the values to process (not modified)minGroupSize- The minimum size of a group (>=1)
-
-
Method Details
-
findRemovalSequence
Finds the minimum sequence of groups needed to remove all cells in the matrix.- Returns:
- A list of Groups representing the removal sequence.
-
printRemovalSequence
Utility method to pretty-print the result of findRemovalSequence().- Type Parameters:
T- The type of values in the matrix (e.g., String, Integer, etc.).- Parameters:
removalSequence- The list of groups returned by the findRemovalSequence() method.
-
printMatrix
public static <T> void printMatrix(T[][] matrix) Utility method to pretty-print the input matrix.- Type Parameters:
T- The type of values in the matrix (e.g., String, Integer, etc.).- Parameters:
matrix- The matrix to be printed.
-