Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. public class AllSolidRectangles {
  2.  
  3.     //finding all solid rectangles in excel table
  4.     //a rectangle is solid if all his borders have solid lines
  5.     //each point in the grid is assigned a value as:
  6.     //0 - no solid line to the point on the right, no solid line to the point below
  7.     //1 - solid line to the right, no solid line below
  8.     //2 - solid line below, no solid line to the right
  9.     //3 - solid lines in both directions
  10.     //points on the right and bottom boarder of the grid may have values indicating lines outside the rectangle
  11.     // perimeter but this doesn't matter for the solution
  12.  
  13.     private static int[][] point;
  14.  
  15.     public static void main(String[] args) {
  16.         //matrix grid described by number of cells
  17.         int matrixLength = 5;
  18.         int matrixHeight = 3;
  19.  
  20.         initializeMatrix(matrixLength, matrixHeight);
  21.         System.out.println(countSolidRectangles());
  22.     }
  23.  
  24.     private static int countSolidRectangles() {
  25.         int solidRectanglesCount = 0;
  26.  
  27.         //finding all possible top left corners of rectangles
  28.         for (int topLeftY = 0; topLeftY <= point.length - 1; topLeftY++) {
  29.             for (int topLeftX = 0; topLeftX <= point[0].length - 1; topLeftX++) {
  30.                 //check if this top left corner is viable
  31.                 if (point[topLeftY][topLeftX] != 3) {
  32.                     continue;
  33.                 }
  34.  
  35.                 //setting length and height of all possible rectangles
  36.                 for (int height = 1; height < point.length - topLeftY; height++) {
  37.                     for (int length = 1; length < point[0].length - topLeftX; length++) {
  38.                         if (isRectangleSolid(topLeftY, topLeftX, length, height)) {
  39.                             solidRectanglesCount++;
  40.                         }
  41.                     }
  42.                 }
  43.             }
  44.         }
  45.         return solidRectanglesCount;
  46.     }
  47.  
  48.     private static boolean isRectangleSolid(int topLeftY, int topLeftX, int length, int height) {
  49.         boolean isSolid = true;
  50.  
  51.         //checking horizontal lines, value of 0 or 2 for a point means no line to the right
  52.         for (int i = 0; i < length; i++) {
  53.             if (point[topLeftY][topLeftX + i] == 0 || point[topLeftY][topLeftX + i] == 2) {
  54.                 isSolid = false;
  55.                 break;
  56.             }
  57.             if (point[topLeftY + height][topLeftX + i] == 0 || point[topLeftY + height][topLeftX + i] == 2) {
  58.                 isSolid = false;
  59.                 break;
  60.             }
  61.         }
  62.         //checking vertical lines, value of 0 or 1 for a point means no line down
  63.         for (int i = 0; i < height; i++) {
  64.             if (point[topLeftY + i][topLeftX] == 0 || point[topLeftY + i][topLeftX] == 1) {
  65.                 isSolid = false;
  66.                 break;
  67.             }
  68.             if (point[topLeftY + i][topLeftX + length] == 0 || point[topLeftY + i][topLeftX + length] == 1) {
  69.                 isSolid = false;
  70.                 break;
  71.             }
  72.         }
  73.         return isSolid;
  74.     }
  75.  
  76.     private static void initializeMatrix(int matrixLength, int matrixHeight) {
  77.         point = new int[matrixHeight + 1][matrixLength + 1];
  78.  
  79.         //initializing solid grid, each point gets a value of 3
  80.         for (int i = 0; i <= matrixHeight; i++) {
  81.             for (int j = 0; j <= matrixLength; j++) {
  82.                 point[i][j] = 3;
  83.             }
  84.         }
  85.  
  86.         //initializing the points which miss at least one solid line below or right
  87.         point[2][1] = 2;
  88.         point[1][2] = 1;
  89.         point[1][3] = 1;
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement