countSubRectangles
подсчитывает height
x width
прямоугольников.
static boolean areAllZeros(int[][] matrix, int top, int left, int height, int width) {
int maxHeight = matrix.length;
int maxWidth = matrix[0].length;
int bottom = top + height;
int right = left + width;
if (bottom > maxHeight || right > maxWidth)
return false;
for (int i = top; i < bottom; ++i)
for (int j = left; j < right; ++j)
if (matrix[i][j] != 0)
return false;
return true;
}
static int countSubRectangles(int[][] matrix, int height, int width) {
int maxHeight = matrix.length;
int maxWidth = matrix[0].length;
int count = 0;
for (int i = 0; i < maxHeight; ++i)
for (int j = 0; j < maxWidth; ++j)
if (areAllZeros(matrix, i, j, height, width))
++count;
return count;
}
и
int[][] matrix = {
{1, 0, 0, 0, 1, 0, 0, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{0, 0, 1, 0, 0, 0, 1, 0},
};
System.out.println(
countSubRectangles(matrix, 1, 2) // count 1x2 sub rectangles
+ countSubRectangles(matrix, 2, 1)); // count 2x1 sub rectangles
// -> 11