https://www.acmicpc.net/problem/2477
특정 알고리즘을쓰는것이 아닌 구현문제에서는 답을 구하는데에 있어서 패턴을 찾는것이 중요하다.
1. 반시계방향으로 돌아야 정상인데 갑자기 시계방향으로 도는 부분이 생긴다 -> 그곳이 빈공간이기 때문에 빈공간 계산
2.만약 다돌았는데 시계방향으로 도는 부분이 없다 -> 내가 우연히 맨 처음 시작점을 시계방향으로 도는 부분으로 정했다.
이렇게 케이스를 두개로 나누었다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Main {
static int count;
static ArrayList<Node> list = new ArrayList<>();
static int height = Integer.MIN_VALUE;
static int width = Integer.MIN_VALUE;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
count = Integer.parseInt(br.readLine());
int prev = -1;
int diff = -1;
for(int i = 0 ; i < 6 ; i++){
String[] lines = br.readLine().split(" ");
int currentDirection = Integer.parseInt(lines[0]);
int currentLength = Integer.parseInt(lines[1]);
list.add(new Node(currentDirection, currentLength));
// 가로, 세로 구하기
if(currentDirection == 4 || currentDirection== 3){
height = Math.max(height, currentLength);
}else {
width = Math.max(width, currentLength);
}
//시계방향으로 꺾이는지 확인 -> 꺾이면 빈공간 계산
if(i > 0 && currentDirection == 1 && prev == 4){
diff = currentLength * list.get(i - 1).length;
}else if(i > 0 && currentDirection == 2 && prev == 3){
diff = currentLength * list.get(i - 1).length;
}else if(i > 0 && currentDirection == 3 && prev == 1){
diff = currentLength * list.get(i - 1).length;
}else if(i > 0 && currentDirection == 4 && prev == 2){
diff = currentLength * list.get(i - 1).length;
}
//
prev = currentDirection;
}
if(diff == -1){
diff = list.get(0).length * list.get(5).length;
}
System.out.println(count * (width * height - diff));
}
}
class Node{
int direction;
int length;
public Node(int d, int l){
direction = d;
length = l;
}
}
'PS > 구현' 카테고리의 다른 글
[JAVA] 백준 2608 - 로마숫자 (1) | 2024.10.16 |
---|