将彩色框分组为正方形的算法

问题描述

假设一个二维 (width * height) 数组,其中每个元素都是一个彩色框。

盒子的数量n。所有框的颜色计数限制为常数 cc <<< n

现在对于给定的 k,想办法将这些框分组为更大的方块,使所有组(方块)的数量最接近 k,其中组项可以是 1,4,9,16,25,36,... 盒子里面(这样它们可以形成一个正方形)。

在每个组(正方形)内,元素必须全部为相同颜色。 单元素方块是有效的。 正方形不能重叠。

解决方法

list all 2 by 2 squares of same colored boxes
while count of squares != k
  if count < k
      if possible to split largest square into smaller squares
          split
      else
          stop
  else 
      if possible to combine 4 small squares into one
          combine
      else
          stop