posite 2026. 4. 6. 12:07

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

 

주어진 X, Y 좌표 및 W, H, 사람들의 좌표가 주어질 때 하키 경기장 안에 있는 사람들의 수를 구하는 문제이다. 하키 경기장의 모양은 (X, Y)가 가장 왼쪽 아래 모서리 이며, 너비 * 높이 크기의 직사각형과 양쪽 끝에 중심이 각각(X, Y + H/2), (X+W, Y + H/2)이면서 반지름이 H/2인 원이 있는 형태이다.

 

각 사람의 좌표가 직사각형에 있는지 먼저 확인 한 후, 있으면 사람의 수를 증가시키고 없으면 양쪽 원 안에 있는지 사람의 좌표와 원의 중심과의 거리를 구해서 반지름 보다 작거나 같으면 원 안에 있는 것이므로 사람의 수를 증가 시킨다.

double rectXEnd = x + w, rectYEnd = y + h;
double firstCenterX = x, firstCenterY = y + h / 2;
double secondCenterX = rectXEnd, secondCenterY = y + h / 2;

for (int i = 0; i < p; i++) {
    st = new StringTokenizer(br.readLine());
    double pX = Double.parseDouble(st.nextToken()), pY = Double.parseDouble(st.nextToken());
    if (pX >= x && pX <= rectXEnd && pY >= y && pY <= rectYEnd) {
        count++;
        continue;
    }
    if (Math.sqrt(Math.pow(pX - firstCenterX, 2) + Math.pow(pY - firstCenterY, 2)) <= h / 2) {
        count++;
        continue;
    }
    
    if (Math.sqrt(Math.pow(pX - secondCenterX, 2) + Math.pow(pY - secondCenterY, 2)) <= h / 2) {
        count++;
    }
}

 

 

결과 코드는 다음과 같다.

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

public class 하키1358 {
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        double w = Double.parseDouble(st.nextToken()), h = Double.parseDouble(st.nextToken());
        double x = Double.parseDouble(st.nextToken()), y = Double.parseDouble(st.nextToken());
        int p = Integer.parseInt(st.nextToken());
        br.close();
        double rectXEnd = x + w, rectYEnd = y + h;
        double firstCenterX = x, firstCenterY = y + h / 2;
        double secondCenterX = rectXEnd, secondCenterY = y + h / 2;
        int count = 0;
        for (int i = 0; i < p; i++) {
            st = new StringTokenizer(br.readLine());
            double pX = Double.parseDouble(st.nextToken()), pY = Double.parseDouble(st.nextToken());
            if (pX >= x && pX <= rectXEnd && pY >= y && pY <= rectYEnd) {
                count++;
                continue;
            }
            if (Math.sqrt(Math.pow(pX - firstCenterX, 2) + Math.pow(pY - firstCenterY, 2)) <= h / 2) {
                count++;
                continue;
            }
            
            if (Math.sqrt(Math.pow(pX - secondCenterX, 2) + Math.pow(pY - secondCenterY, 2)) <= h / 2) {
                count++;
            }
        }
        System.out.print(count);
    }
}