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.).

public class MatrixGroupsRemover<T> extends Object
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.

  • Constructor Details

    • MatrixGroupsRemover

      public MatrixGroupsRemover(T[][] grid, int minGroupSize)
      Parameters:
      grid - The matrix containing the values to process (not modified)
      minGroupSize - The minimum size of a group (>=1)
  • Method Details

    • findRemovalSequence

      public List<MatrixGroupsRemover.Group<T>> 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

      public static <T> void printRemovalSequence(List<MatrixGroupsRemover.Group<T>> removalSequence)
      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.