"꾸준하고 완벽한 한 걸음"

PS/Baekjoon Online Judge

[백준 02121] 넷이 놀기 [Java]

kimyoungrok 2025. 4. 18. 22:58
728x90

문제

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

 


풀이

N개의 점에 대해 x, y 축에 평행하는 직사각형의 수를 찾는 문제다.

좌표를 한 쌍으로 묶어 집합에 넣어주고,

        Set<Point> set = new HashSet<>();
        for (int i = 0; i < N; ++i) {
            input = br.readLine().split(" ");
            final int x = Integer.parseInt(input[0]);
            final int y = Integer.parseInt(input[1]);

            // Solve
            set.add(new Point(x, y));
        }

문제의 조건에 부합하는 좌표와 일치하는지 비교하는 방식으로 풀이했다.

        long cnt = 0;
        for (Point p : set) {
            if (set.contains(new Point(p.x + A, p.y)) &&
                    set.contains(new Point(p.x, p.y + B)) &&
                    set.contains(new Point(p.x + A, p.y + B))) {
                ++cnt;
            }
        }

풀이 시간

≤ 15


소스코드

https://github.com/rogi-rogi/problem-solving/blob/main/baekjoon-online-judge/easy/02121.java

 

problem-solving/baekjoon-online-judge/easy/02121.java at main · rogi-rogi/problem-solving

Daily Problem Solving Challenges. Contribute to rogi-rogi/problem-solving development by creating an account on GitHub.

github.com

 

728x90