[백준] 2477번 Java (수학, 구현)

동은·2024년 9월 20일
post-thumbnail

https://www.acmicpc.net/problem/2477

💡문제

풀이

접근법 : 육각형의 임의의 한 꼭짓점에서 출발하여 반시계방향으로 둘레를 돈다!
즉 가로 다음엔 세로, 세로 다음엔 가로 값이 나오게 된다.
밭의 넓이 = 전체 크기(최대 세로 * 최대 가로) - 작은 사각형의 크기(최소 세로 * 최소 가로)

  1. 전체 크기 구하기
  • 입력 받은 k에 따라 변의 방향을 알 수 있다. 변의 방향 = 가로인지 세로인지.
  • 변의 방향값을 구한 뒤 최대 가로와 최대 세로의 길이, index를 구한다.

  1. 제외할 작은 사각형의 크기 구하기
  • 최대 세로의 이전 인덱스(preIdx), 이후 인덱스(nextIdx) 중 하나는 반드시 최대 가로이다.
  • 최대 가로가 preIdx라면 preIdx - nextIdx = 최소 가로
  • 따라서 두 인덱스를 빼서 Math.abs로 절댓값을 구하면 최소 가로를 구할 수 있다.

내 코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        int n = Integer.parseInt(br.readLine());

        int[] length = new int[6];
        int maxWidth = 0, maxWidthI = 0, maxHeight = 0, maxHeightI = 0;

        for (int i = 0; i < 6; i++) {
            st = new StringTokenizer(br.readLine());
            int direction = Integer.parseInt(st.nextToken()); //방향
            length[i] = Integer.parseInt(st.nextToken());
            // 가로 값
            if (direction == 1 || direction == 2) {
                // 최대 가로, 최대 가로의 인덱스 구하기
                if (maxHeight < length[i]) {
                    maxHeight = length[i];
                    maxHeightI = i;
                }
             // 세로 값
            } else if (direction == 3 || direction == 4) {
            	// 최대 세로, 최대 세로의 인덱스 구하기 
                if (maxWidth < length[i]) {
                    maxWidth = length[i];
                    maxWidthI = i;
                }
            }
        }

        // 제외할 사각형의 가로 세로 찾기
        int minWidth = 0, minHeight = 0;	// 최소 가로,세로
        for (int i = 0; i < 6; i++) {
            //index가 0보다 작거나 6보다 큰 경우 나눠서 설정하기
            int preIdx = (i - 1) < 0 ? 5 : i - 1;   // 이전 인덱스
            int nextIdx = (i + 1) >= 6 ? 0 : i + 1; // 이후 인덱스

            if (i == maxHeightI) {
                minHeight = Math.abs(length[preIdx] - length[nextIdx]);
            } else if (i == maxWidthI) {
                minWidth = Math.abs(length[preIdx] - length[nextIdx]);
            }
        }

        // 전체 면적 = 최대 가로 * 최대 세로
        // 제외할 면적 = 최소 가로 * 최소 세로
        System.out.println(n * ((maxWidth * maxHeight) - (minWidth * minHeight)));

    }
}

0개의 댓글