Cybazaar

Gadnite kvadrati :D

Feb 28th, 2020
893
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4.     static final String RECTANGLE_TYPE = "%dx%d: %d%n";
  5.     static final String RECTANGLE_INFO = "%dx%d (%d:%d - %d:%d)";
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         int n = Integer.parseInt(scanner.nextLine());
  10.  
  11.         int[] firstCell = Arrays.stream(scanner.nextLine().split("\\s+")).mapToInt(Integer::parseInt).toArray();
  12.         int[] secondCell = Arrays.stream(scanner.nextLine().split("\\s+")).mapToInt(Integer::parseInt).toArray();
  13.  
  14.  
  15.         Set<String> set = new LinkedHashSet<>();
  16.  
  17.         int counter = 0;
  18.         int total = 0;
  19.         for (int i = n; i >= 1; i--) {
  20.             for (int j = i; j >= 1; j--) {
  21.                 System.out.print("");
  22.  
  23.                 //  vertical counting and storing
  24.                 for (int k = 0; k < n; k++) {
  25.                     for (int l = 0; l < n; l++) {
  26.                         System.out.print("");
  27.                         if (k + i <= n && l + j <= n) {
  28.                             if (!isRectangle(i, j, k, l, k + i, l + j, firstCell[0], firstCell[1], secondCell[0], secondCell[1])) {
  29.                                 continue;
  30.                             }
  31.                             if (set.add(String.format(RECTANGLE_INFO, i, j, k, l, k + i, l + j))) {
  32.                                 counter++;
  33.                             }
  34.                         }
  35.                     }
  36.                 }
  37.  
  38.                 //  horizontal counting and storing
  39.                 for (int k = 0; k < n; k++) {
  40.                     for (int l = 0; l < n; l++) {
  41.                         if (k + j <= n && l + i <= n) {
  42.                             if (!isRectangle(i, j, k, l, k + j, l + i, firstCell[0], firstCell[1], secondCell[0], secondCell[1])) {
  43.                                 continue;
  44.                             }
  45.                             if (set.add(String.format(RECTANGLE_INFO, i, j, k, l, k + j, l + i))) {
  46.                                 counter++;
  47.                             }
  48.                         }
  49.                     }
  50.                 }
  51.  
  52.                 System.out.printf(RECTANGLE_TYPE, i, j, counter);
  53.                 total += counter;
  54.                 counter = 0;
  55.             }
  56.         }
  57.         System.out.println(total);
  58.  
  59.     }
  60.  
  61.     private static boolean isRectangle(int length, int width, int fromRow, int fromCol, int toRow, int toCol, int lineFromRow, int lineFromCol, int lineToRow, int lineToCol) {
  62.  
  63.         // filler strings just to keep track of the lines
  64.         String topSide = String.format("%d:%d - %d:%d", fromRow, fromCol, toRow - length, toCol);
  65.         String bottomSide = String.format("%d:%d - %d:%d", fromRow + length, fromCol, toRow, toCol);
  66.         String leftSide = String.format("%d:%d - %d:%d", fromRow, fromCol, toRow, toCol - width);
  67.         String rightSide = String.format("%d:%d - %d:%d", fromRow, fromCol + width, toRow, toCol);
  68.  
  69.         String line = String.format("%d:%d - %d:%d", lineFromRow, lineFromCol, lineToRow, lineToCol);
  70.  
  71.         if (lineFromRow == lineToRow) { //  line is horizontal
  72.             if (lineFromRow == fromRow || lineFromRow == toRow) { //    top
  73.                 return !isSegment(fromCol, toCol, lineFromCol, lineToCol);
  74.             }
  75.         } else if (lineFromCol == lineToCol) { //   line is vertical
  76.             if (lineFromCol == fromCol || lineFromCol == toCol) {
  77.                 return !isSegment(fromRow, toRow, lineFromRow, lineToRow);
  78.             }
  79.         }
  80.  
  81.         return true;
  82.     }
  83.  
  84.     private static boolean isSegment(int from, int to, int lineFrom, int lineTo) {
  85.         return from <= lineFrom && lineTo <= to;
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment