Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AllSolidRectangles {
- //finding all solid rectangles in excel table
- //a rectangle is solid if all his borders have solid lines
- //each point in the grid is assigned a value as:
- //0 - no solid line to the point on the right, no solid line to the point below
- //1 - solid line to the right, no solid line below
- //2 - solid line below, no solid line to the right
- //3 - solid lines in both directions
- //points on the right and bottom boarder of the grid may have values indicating lines outside the rectangle
- // perimeter but this doesn't matter for the solution
- private static int[][] point;
- public static void main(String[] args) {
- //matrix grid described by number of cells
- int matrixLength = 5;
- int matrixHeight = 3;
- initializeMatrix(matrixLength, matrixHeight);
- System.out.println(countSolidRectangles());
- }
- private static int countSolidRectangles() {
- int solidRectanglesCount = 0;
- //finding all possible top left corners of rectangles
- for (int topLeftY = 0; topLeftY <= point.length - 1; topLeftY++) {
- for (int topLeftX = 0; topLeftX <= point[0].length - 1; topLeftX++) {
- //check if this top left corner is viable
- if (point[topLeftY][topLeftX] != 3) {
- continue;
- }
- //setting length and height of all possible rectangles
- for (int height = 1; height < point.length - topLeftY; height++) {
- for (int length = 1; length < point[0].length - topLeftX; length++) {
- if (isRectangleSolid(topLeftY, topLeftX, length, height)) {
- solidRectanglesCount++;
- }
- }
- }
- }
- }
- return solidRectanglesCount;
- }
- private static boolean isRectangleSolid(int topLeftY, int topLeftX, int length, int height) {
- boolean isSolid = true;
- //checking horizontal lines, value of 0 or 2 for a point means no line to the right
- for (int i = 0; i < length; i++) {
- if (point[topLeftY][topLeftX + i] == 0 || point[topLeftY][topLeftX + i] == 2) {
- isSolid = false;
- break;
- }
- if (point[topLeftY + height][topLeftX + i] == 0 || point[topLeftY + height][topLeftX + i] == 2) {
- isSolid = false;
- break;
- }
- }
- //checking vertical lines, value of 0 or 1 for a point means no line down
- for (int i = 0; i < height; i++) {
- if (point[topLeftY + i][topLeftX] == 0 || point[topLeftY + i][topLeftX] == 1) {
- isSolid = false;
- break;
- }
- if (point[topLeftY + i][topLeftX + length] == 0 || point[topLeftY + i][topLeftX + length] == 1) {
- isSolid = false;
- break;
- }
- }
- return isSolid;
- }
- private static void initializeMatrix(int matrixLength, int matrixHeight) {
- point = new int[matrixHeight + 1][matrixLength + 1];
- //initializing solid grid, each point gets a value of 3
- for (int i = 0; i <= matrixHeight; i++) {
- for (int j = 0; j <= matrixLength; j++) {
- point[i][j] = 3;
- }
- }
- //initializing the points which miss at least one solid line below or right
- point[2][1] = 2;
- point[1][2] = 1;
- point[1][3] = 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement