Dev
BAN USERpublic class MatrixArea {
class Rect {
int row;
int col;
int width;
int height;
int area;
}
public static void main(String[] args) {
int matrix[][] = { { 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1 },
{ 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0 },
{ 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
{ 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1 },
{ 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0 } };
findArea(matrix);
}
private static void findArea(int[][] input) {
Rect maxArea = new MatrixArea().new Rect();
int row = input.length;
int col = input[0].length;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (input[i][j] == 0) {
Rect rect = matrixArea(input, i, j);
System.out.print(rect.area + " ");
if (rect.area > maxArea.area) {
maxArea = rect;
maxArea.row = i;
maxArea.col = j;
}
} else {
System.out.print(" ");
}
}
System.out.println();
}
System.out.println("Max Area " + maxArea.area + " (" + maxArea.width
+ " * " + maxArea.height + ") from index " + maxArea.row + ","
+ maxArea.col);
}
private static Rect matrixArea(int[][] input, int x, int y) {
int row = input.length;
int col = input[0].length;
int width = 0;
int height = 0;
int maxArea = 0;
Rect rect = new MatrixArea().new Rect();
for (int i = x; i < row; i++) {
if (input[i][y] != 0) {
break;
}
int curWidth = 0;
for (int j = y; j < col; j++) {
if (input[i][j] == 0) {
curWidth++;
} else {
break;
}
}
if (curWidth == 0)
break;
if (curWidth < width || width == 0)
width = curWidth;
height++;
int area = height * width;
if (area > maxArea) {
rect.width = width;
rect.height = height;
rect.area = area;
maxArea = area;
}
}
return rect;
}
}
Can you please explain Step 2 ?
- Dev October 28, 2013How you got 1, 2, 3, 2, 3, 2, 1, 0