본문 바로가기
99클럽 코테 스터디

[99클럽 코테 스터디 10일차 TIL] 평행선

by sozr 2025. 4. 13.

 

 

 

오늘의 키워드

  • HashMap

 

 

백준 2358번: 평행선

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

 

 

 

 

문제

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

 

 

입렵예제

4
0 0
10 0
0 10
10 10

 

 

 

출력예제

4

 

 

 

풀이

 

1. x축 y축을 담을 HashMap 2개를 선언한다.

2. key값은 x축, y축 value 값은 개수

3. HashMap.values()를 통해 2개 이상이면 count++ 해준다.

 

 

 

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

class Main {
    public static void main(String[] args) throws IOException {
        int count = 0;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        HashMap<Integer, Integer> xmap= new HashMap<>();
        HashMap<Integer, Integer> ymap = new HashMap<>();
        
        StringTokenizer st;
        
        int n = Integer.parseInt(br.readLine());
        
        while(n-- > 0){
            st = new StringTokenizer(br.readLine());
            int x = Integer.parseInt(st.nextToken());
            int y = Integer.parseInt(st.nextToken());

            xmap.put(x, xmap.getOrDefault(x, 0) + 1);
            ymap.put(y, ymap.getOrDefault(y, 0) + 1);
            
        }
        
        for(int x : xmap.values()) {
            if(x > 1) count++;
        }
        
        for(int y : ymap.values()){
            if(y > 1) count++;
        }
        
        System.out.print(count);
    }
}