백준 1004 어린 왕자[Java]

seren-dev·2022년 9월 2일
0

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

풀이

출발점과 속한 원의 개수와 도착점이 속한 원의 개수를 더한다. 단, 두 점이 같은 원에 속할 경우 그 원은 제외한다.

코드

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

public class Main {

    public static double distancePow(int x1, int y1, int x2, int y2) {
        return (Math.pow(x1-x2, 2) + Math.pow(y1-y2, 2));
    }

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

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

        StringBuilder sb = new StringBuilder();

        while(t-- > 0) {
            int cnt = 0;
            StringTokenizer st = new StringTokenizer(br.readLine());

            int x1 = Integer.parseInt(st.nextToken());
            int y1 = Integer.parseInt(st.nextToken());
            int x2 = Integer.parseInt(st.nextToken());
            int y2 = Integer.parseInt(st.nextToken());

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

            for (int i = 0; i < n; i++) {
                st = new StringTokenizer(br.readLine());
                int x = Integer.parseInt(st.nextToken());
                int y = Integer.parseInt(st.nextToken());
                int r = Integer.parseInt(st.nextToken());

                double d1 = distancePow(x,y,x1,y1);
                double d2 = distancePow(x,y,x2,y2);
                r = r*r;

                if (d1 < r && d2 > r) //출발점만 현재 원에 속하는 경우
                    cnt++;
                else if (d1 > r && d2 < r) //도착점만 현재 원에 속하는 경우
                    cnt++;

            }

            sb.append(cnt+"\n");
        }

        System.out.println(sb);
    }
}

0개의 댓글