Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- 스프링부트
- N+1 문제
- 로그인
- 백준
- OOP
- 성능 개선
- 프로토콜
- RDS
- 인메모리
- 트러블 슈팅
- redis
- Java
- JMeter
- JWT
- 동시성 문제
- 객체지향
- Spring Security
- 코딩테스트
- TCP/IP
- 4-way-handshake
- 알고리즘
- Spring
- docker
- 형상관리
- 3-way-handshake
- OSI 7계층
- AWS
- EC2
- 네트워크
Archives
- Today
- Total
열공스토리
[백준] 18808 - 스티커 붙이기 (JAVA 객체지향적으로 설계해 보기) 본문
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
static int N;
static int M;
static class Input {
private final Scanner scanner;
public Input() {
this.scanner = new Scanner(System.in);
}
public int inputLaptopRow() {
return scanner.nextInt();
}
public int inputLaptopCol() {
return scanner.nextInt();
}
public int inputStickerCount() {
return scanner.nextInt();
}
public List<int[]> inputSticker() {
int R = scanner.nextInt();
int C = scanner.nextInt();
List<int[]> list = new ArrayList<>();
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
if (scanner.nextInt() == 1) {
list.add(new int[]{i, j});
}
}
}
return list;
}
}
static class Sticker {
private List<int[]> coordinate; // int[0] : x, int[1] : y
private int maxRow;
public Sticker(List<int[]> coordinate) {
this.coordinate = coordinate;
this.maxRow = setMaxRow();
}
public void rotateQuarterClockwise() {
List<int[]> coordinateAfterRotate = new ArrayList<>();
for (int[] i : coordinate) {
coordinateAfterRotate.add(new int[]{i[1], maxRow - i[0]});
}
this.coordinate = coordinateAfterRotate;
this.maxRow = setMaxRow();
}
public boolean isPossibleAttach(int r, int c, Laptop laptop) {
return coordinate.stream()
.allMatch(e -> sizeConstraint(r + e[0], c + e[1])
&& laptop.isExistSpace(r + e[0], c + e[1])
);
}
private boolean sizeConstraint(int r, int c) {
return r < N && c < M;
}
private int setMaxRow() {
return coordinate.stream()
.map(e -> e[0])
.max(Integer::compareTo)
.get();
}
}
static class Laptop {
private int map[][];
public Laptop(int[][] map) {
this.map = map;
}
public boolean isAttachSticker(Sticker sticker) {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if (sticker.isPossibleAttach(i, j, this)) {
int finalI = i;
int finalJ = j;
sticker.coordinate.stream()
.forEach(e -> attach(e[0] + finalI, e[1] + finalJ));
return true;
}
}
}
return false;
}
private void attach(int r, int c) {
map[r][c] = 1;
}
public boolean isExistSpace(int r, int c) {
if (map[r][c] == 0) {
return true;
}
return false;
}
public int getTotalSquare() {
return (int) Arrays.stream(map)
.flatMapToInt(Arrays::stream)
.filter(e -> e == 1)
.count();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i[] : map) {
sb.append(Arrays.toString(i) + "\\n");
}
return sb.toString();
}
}
public static void main(String[] args) {
Input input = new Input();
N = input.inputLaptopRow();
M = input.inputLaptopCol();
int K = input.inputStickerCount();
Laptop laptop = new Laptop(new int[N][M]);
while (K-- > 0) {
Sticker sticker = new Sticker(input.inputSticker());
int rotationCount = 4;
while (rotationCount-- > 0) {
if (laptop.isAttachSticker(sticker)) {
break;
}
sticker.rotateQuarterClockwise();
}
}
System.out.println(laptop.getTotalSquare());
}
}
'백준' 카테고리의 다른 글
[백준] 23288 - 주사위 굴리기2 (JAVA 객체지향적으로 설계해 보기) (0) | 2023.11.17 |
---|---|
[백준] 15562 - 톱니바퀴2 (JAVA 객체지향적으로 설계해 보기) (0) | 2023.11.17 |