99클럽 코테 스터디 10일차 TIL + 해시

개발자 춘식이·2025년 4월 11일
0

항해99클럽

목록 보기
10/10

문제

백준 2358번-평행선

평면에 n개의 점이 있다. 그중 두 개 이상의 점을 지나면서 x축 또는 y축에 평행한 직선이 몇 개인지 알아내는 프로그램을 작성하시오.

입력:
첫째 줄에 n(1 ≤ n ≤ 100,000)이 주어진다. 다음 n개의 줄에는 각 점의 좌표가 주어진다. 같은 좌표가 여러 번 주어질 수 있으며, 그런 경우 서로 다른 점으로 간주한다. 좌표는 절댓값이 231보다 작은 정수이다.

출력:
첫째 줄에 답을 출력한다.

예시

입력:

4
0 0
10 10
0 10
10 0

출력:

4

풀이

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

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

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

        Map<Integer, Integer> y = new HashMap<>(), x = new HashMap<>();
        while (n-- > 0) {
            st = new StringTokenizer(br.readLine());
            int input = Integer.parseInt(st.nextToken());
            if (y.containsKey(input))
                y.put(input, y.get(input) + 1);
            else
                y.put(input, 0);

            input = Integer.parseInt(st.nextToken());
            if (x.containsKey(input))
                x.put(input, x.get(input) + 1);
            else
                x.put(input, 0);
        }

        int cnt = 0;
        for (int key : y.keySet())
            if (y.get(key) > 0)
                cnt++;

        for (int key : x.keySet())
            if (x.get(key) > 0)
                cnt++;

        bw.write(String.valueOf(cnt));
        bw.flush();
    }
}

회고

참고: https://graycode.tistory.com/350

profile
춘식이를 너무 좋아하는 주니어 백엔드 개발자입니다.

0개의 댓글