백준 2447 참외밭[Java]⭐

seren-dev·2022년 9월 2일
0

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

풀이

가장 긴 가로, 세로 길이를 구하고 곱하여 직사각형의 넓이를 구하고 그 넓이에서 빈 직사각형의 넓이를 빼야 한다.
빈 직사각형의 가로와 세로를 구하는 방법은 다음과 같다.

가장 긴 가로 옆에 위치한 두 세로의 길이 차이가 빈 사각형의 세로가 된다.
같은 원리로 가장 긴 세로 양 옆에 위치한 두 가로의 차이가 빈 사각형의 가로가 된다.

코드

import java.io.*;
        import java.util.*;

public class Main {

    static class Line {
        int dir, num;
        public Line(int dir, int num) {
            this.dir = dir;
            this.num = num;
        }

        @Override
        public boolean equals(Object o) {
            Line ob = (Line) o;
            if (this.dir == ob.dir && this.num == ob.num)
                return true;
            else return false;
        }
    }

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int k = Integer.parseInt(br.readLine());

        ArrayList<Line> list = new ArrayList<>();

        int maxWidth = 0, maxHeight = 0;
        Line wLine=null, hLine=null;

        for (int i = 0; i < 6; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int dir = Integer.parseInt(st.nextToken());
            int num = Integer.parseInt(st.nextToken());

            list.add(new Line(dir, num));

            if (dir == 1 || dir == 2) {
                //가로 중 가장 긴 선분
                if (maxWidth < num) {
                    maxWidth = Math.max(maxWidth, num);
                    wLine = new Line(dir, num);
                }
            }
            else {
                //세로 중 가장 긴 선분
                if (maxHeight < num) {
                    maxHeight = Math.max(maxHeight, num);
                    hLine = new Line(dir, num);
                }
            }
        }

        //가로 중 가장 긴 선분 옆에 위치한 세로의 길이 차를 구함
        int wIdx = list.indexOf(wLine);
        int h = Math.abs(list.get((wIdx+5)%6).num - list.get((wIdx+1)%6).num);

        //세로 중 가장 긴 선분 옆에 위치한 가로의 길이 차를 구함
        int hIdx = list.indexOf(hLine);
        int w = Math.abs(list.get((hIdx+5)%6).num - list.get((hIdx+1)%6).num);

        System.out.println((maxWidth*maxHeight - w*h) * k);
    }
}
  • static class Line을 선언하여 방향과 길이를 저장한다.
  • public boolean equals(Object o)를 Override한다.
    • indexOf() 메서드는 equals() 메소드를 사용하여 객체를 찾기 때문이다.

다른 풀이

import java.io.*;
        import java.util.*;

public class Main {

    static class Line {
        int dir, num;
        public Line(int dir, int num) {
            this.dir = dir;
            this.num = num;
        }
    }

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int k = Integer.parseInt(br.readLine());

        ArrayList<Line> list = new ArrayList<>();

        int maxWidth = 0, maxHeight = 0, maxWidthIdx = 0, maxHeightIdx=0;

        for (int i = 0; i < 6; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int dir = Integer.parseInt(st.nextToken());
            int num = Integer.parseInt(st.nextToken());

            list.add(new Line(dir, num));

            if (dir == 1 || dir == 2) {
                //가로 중 가장 긴 선분
                if (maxWidth < num) {
                    maxWidth = Math.max(maxWidth, num);
                    maxWidthIdx = i;
                }
            }
            else {
                //세로 중 가장 긴 선분
                if (maxHeight < num) {
                    maxHeight = Math.max(maxHeight, num);
                    maxHeightIdx = i;
                }
            }
        }

        //가로 중 가장 긴 선분 옆에 위치한 세로의 길이 차를 구함
        int h = Math.abs(list.get((maxWidthIdx+5)%6).num - list.get((maxWidthIdx+1)%6).num);

        //세로 중 가장 긴 선분 옆에 위치한 가로의 길이 차를 구함
        int w = Math.abs(list.get((maxHeightIdx+5)%6).num - list.get((maxHeightIdx+1)%6).num);

        System.out.println((maxWidth*maxHeight - w*h)*k);
    }
}
  • equals() 메서드를 Override 할 필요 없이 인덱스도 따로 저장해 놓는다,

0개의 댓글