본문 바로가기

코딩 테스트/백준

[백준 1004번 문제, JAVA] 어린 왕자

728x90
반응형

문제


코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        List<Integer> answerList = new ArrayList<>();
        
        int n = Integer.parseInt(br.readLine());
        for (int i = 0; i < n; i++) {
            answerList.add(0);
            String[] input = br.readLine().split(" ");
            List<Point> pointList = new ArrayList<>();
            pointList.add(new Point(Integer.parseInt(input[0]), Integer.parseInt(input[1])));
            pointList.add(new Point(Integer.parseInt(input[2]), Integer.parseInt(input[3])));
            
            int plantCount = Integer.parseInt(br.readLine());
            List<Planet> planetList = new ArrayList<>();
            for(int j = 0; j < plantCount; j++) {
                String[] planetInput = br.readLine().split(" ");
                planetList.add(new Planet(Integer.parseInt(planetInput[0]), Integer.parseInt(planetInput[1]), Integer.parseInt(planetInput[2])));
            }
            for(Planet planet : planetList) {
                if (!(planet.isInside(pointList.get(0).x, pointList.get(0).y) && planet.isInside(pointList.get(1).x, pointList.get(1).y))
                    && (planet.isInside(pointList.get(0).x, pointList.get(0).y) || planet.isInside(pointList.get(1).x, pointList.get(1).y))) {
                    answerList.set(i, answerList.get(i) + 1);
                }
            }
        }
        for (Integer integer : answerList) {
            System.out.println(integer);
        }
    }
    static class Point {
        int x, y;
        Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
    static class Planet {
        int x, y, r;
        Planet(int x, int y, int r) {
            this.x = x;
            this.y = y;
            this.r = r;
        }
        boolean isInside(int x, int y) {
            if(Math.pow(r, 2) >= (Math.pow(x - this.x, 2) + Math.pow(y - this.y, 2))) {
                return true;
            } else {
                return false;
            }
        }
    }
}

해결

시작 점과 끝 점을 각각 포괄하는 행성계(원)의 개수를 구하면 된다.

만약, 시작 점과 끝점이 동시에 포괄되는 행성계가 있다면 포함하지 않는다.


참고

https://dundung.tistory.com/152

 

 

728x90
반응형