Created
May 5, 2020 13:06
-
-
Save madsunrise/93571e2670a1a536e76b2df6f62b5082 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
int width = scanner.nextInt(); | |
int height = scanner.nextInt(); | |
int advertCount = scanner.nextInt(); | |
List<Advert> advertList = new ArrayList<>(advertCount); | |
for (int i = 0; i < advertCount; ++i) { | |
Advert advert = new Advert( | |
scanner.nextInt(), | |
scanner.nextInt(), | |
scanner.nextInt(), | |
scanner.nextInt() | |
); | |
advertList.add(advert); | |
} | |
Square maxSquare = findSquare(width, height, advertList); | |
System.out.print(maxSquare.col); | |
System.out.print(' '); | |
System.out.print(maxSquare.row); | |
System.out.print(' '); | |
System.out.print(maxSquare.col + maxSquare.size); | |
System.out.print(' '); | |
System.out.print(maxSquare.row + maxSquare.size); | |
} | |
private static Square findSquare(int width, int height, List<Advert> adverts) { | |
int maxLength = Math.max(width, height); | |
for (int i = maxLength; i >= 1; i--) { | |
Square square = findSquareWithSize(width, height, adverts, i); | |
if (square != null) { | |
return square; | |
} | |
} | |
return null; | |
} | |
private static Square findSquareWithSize(int width, int height, List<Advert> adverts, int squareSize) { | |
int maxRow = height - squareSize + 1; | |
int maxCol = width - squareSize + 1; | |
for (int row = 0; row < maxRow; row++) { | |
for (int col = 0; col < maxCol; col++) { | |
if (isSquare(row, col, adverts, squareSize)) { | |
return new Square(row, col, squareSize); | |
} | |
} | |
} | |
return null; | |
} | |
private static boolean isSquare(int rowIdx, int colIdx, List<Advert> adverts, int squareSize) { | |
int x1 = colIdx; | |
int y1 = rowIdx; | |
int x2 = colIdx + squareSize; | |
int y2 = rowIdx + squareSize; | |
for (Advert advert: adverts) { | |
if (advert.hasIntersectionWith(x1, y1, x2, y2)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
private static class Advert { | |
private final int x1; | |
private final int y1; | |
private final int x2; | |
private final int y2; | |
public Advert(int x1, int y1, int x2, int y2) { | |
this.x1 = x1; | |
this.y1 = y1; | |
this.x2 = x2; | |
this.y2 = y2; | |
} | |
public int getX1() { | |
return x1; | |
} | |
public int getY1() { | |
return y1; | |
} | |
public int getX2() { | |
return x2; | |
} | |
public int getY2() { | |
return y2; | |
} | |
public boolean hasIntersectionWith(int x1, int y1, int x2, int y2) { | |
if (this.x1 >= x2 || this.x2 <= x1) { | |
return false; | |
} | |
// If one rectangle is above other | |
if (this.y1 >= y2 || this.y2 <= y1) { | |
return false; | |
} | |
return true; | |
} | |
} | |
private static class Square { | |
private final int row; | |
private final int col; | |
private final int size; | |
public Square(int row, int col, int size) { | |
this.row = row; | |
this.col = col; | |
this.size = size; | |
} | |
public int getRow() { | |
return row; | |
} | |
public int getCol() { | |
return col; | |
} | |
public int getSize() { | |
return size; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment