본문 바로가기

알고리즘/백준

[백준] 1004번 자바

정답 코드

 

import java.util.Scanner;

public class Baek1004 {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int T;
        int x1,y1, x2,y2;
        double[] d1, d2;
        int n;
        int cont = 0;

        int[] cx, cy, r;

        T = sc.nextInt();
        for(int t = 0 ; t < T;t++){
            cont = 0;
            x1 = sc.nextInt();
            y1 = sc.nextInt();
            x2 = sc.nextInt();
            y2 = sc.nextInt();

            n = sc.nextInt();
            cx = new int[n];
            cy = new int[n];
            r = new int[n];
            d1 = new double[n];
            d2 = new double[n];

            for(int i = 0 ; i < n; i++){
                cx[i] = sc.nextInt();
                cy[i] = sc.nextInt();
                r[i] = sc.nextInt();
            }
            for(int i = 0 ; i < n; i++){
                d1[i] = Math.sqrt(Math.pow(x1-cx[i],2)+Math.pow(y1-cy[i],2));
                d2[i] = Math.sqrt(Math.pow(x2-cx[i],2)+Math.pow(y2-cy[i],2));
            }

            for(int i = 0 ; i < n; i++){
                if(d1[i] < r[i]) // 출발지가 행성계에 포함되는 경우 카운트
                    cont++;
                if(d2[i] < r[i]) // 도착지가 행성계에 포함되는 경우 카운트
                    cont++;
                if(d1[i] < r[i] && d2[i] < r[i]) //출발지와 도착지가 같은 행성계에 있는 경우 카운트를 하지 않는다.
                    cont-=2;                    // 위에서 카운트된 2를 뺀다.
            }
            System.out.println(cont);
        }


    }
}

 

'알고리즘 > 백준' 카테고리의 다른 글

[백준] 1003번 자바  (0) 2022.02.17
[백준] 1002번 자바  (0) 2022.02.17