Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main {
- static final String RECTANGLE_TYPE = "%dx%d: %d%n";
- static final String RECTANGLE_INFO = "%dx%d (%d:%d - %d:%d)";
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- int[] firstCell = Arrays.stream(scanner.nextLine().split("\\s+")).mapToInt(Integer::parseInt).toArray();
- int[] secondCell = Arrays.stream(scanner.nextLine().split("\\s+")).mapToInt(Integer::parseInt).toArray();
- Set<String> set = new LinkedHashSet<>();
- int counter = 0;
- int total = 0;
- for (int i = n; i >= 1; i--) {
- for (int j = i; j >= 1; j--) {
- System.out.print("");
- // vertical counting and storing
- for (int k = 0; k < n; k++) {
- for (int l = 0; l < n; l++) {
- System.out.print("");
- if (k + i <= n && l + j <= n) {
- if (!isRectangle(i, j, k, l, k + i, l + j, firstCell[0], firstCell[1], secondCell[0], secondCell[1])) {
- continue;
- }
- if (set.add(String.format(RECTANGLE_INFO, i, j, k, l, k + i, l + j))) {
- counter++;
- }
- }
- }
- }
- // horizontal counting and storing
- for (int k = 0; k < n; k++) {
- for (int l = 0; l < n; l++) {
- if (k + j <= n && l + i <= n) {
- if (!isRectangle(i, j, k, l, k + j, l + i, firstCell[0], firstCell[1], secondCell[0], secondCell[1])) {
- continue;
- }
- if (set.add(String.format(RECTANGLE_INFO, i, j, k, l, k + j, l + i))) {
- counter++;
- }
- }
- }
- }
- System.out.printf(RECTANGLE_TYPE, i, j, counter);
- total += counter;
- counter = 0;
- }
- }
- System.out.println(total);
- }
- private static boolean isRectangle(int length, int width, int fromRow, int fromCol, int toRow, int toCol, int lineFromRow, int lineFromCol, int lineToRow, int lineToCol) {
- // filler strings just to keep track of the lines
- String topSide = String.format("%d:%d - %d:%d", fromRow, fromCol, toRow - length, toCol);
- String bottomSide = String.format("%d:%d - %d:%d", fromRow + length, fromCol, toRow, toCol);
- String leftSide = String.format("%d:%d - %d:%d", fromRow, fromCol, toRow, toCol - width);
- String rightSide = String.format("%d:%d - %d:%d", fromRow, fromCol + width, toRow, toCol);
- String line = String.format("%d:%d - %d:%d", lineFromRow, lineFromCol, lineToRow, lineToCol);
- if (lineFromRow == lineToRow) { // line is horizontal
- if (lineFromRow == fromRow || lineFromRow == toRow) { // top
- return !isSegment(fromCol, toCol, lineFromCol, lineToCol);
- }
- } else if (lineFromCol == lineToCol) { // line is vertical
- if (lineFromCol == fromCol || lineFromCol == toCol) {
- return !isSegment(fromRow, toRow, lineFromRow, lineToRow);
- }
- }
- return true;
- }
- private static boolean isSegment(int from, int to, int lineFrom, int lineTo) {
- return from <= lineFrom && lineTo <= to;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment