본문 바로가기

코딩 테스트/백준

[백준 1002번 문제, JAVA] 터렛

728x90
반응형

문제


코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        List<Integer> answers = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            String[] tokens = reader.readLine().split(" ");
            Point p1 = new Point(Integer.parseInt(tokens[0]), Integer.parseInt(tokens[1]), Integer.parseInt(tokens[2]));
            Point p2 = new Point(Integer.parseInt(tokens[3]), Integer.parseInt(tokens[4]), Integer.parseInt(tokens[5]));
            answers.add(search(p1, p2));
        }
        for (Integer answer : answers) {
            System.out.println(answer);
        }
    }
    static class Point {
        public Point(double x, double y, double r) {
            this.x = x;
            this.y = y;
            this.r = r;
        }
        double x, y, r;
    }
    static int search(Point turret1, Point turret2) {
        List<Double> list = new ArrayList<>();
        list.add(turret1.r);
        list.add(turret2.r);
        list.add(Math.sqrt(Math.pow(turret2.x - turret1.x, 2) + Math.pow(turret2.y - turret1.y, 2)));
        Double r3 = Collections.max(list);
        list.remove(r3);
        if(turret1.x == turret2.x && turret1.y == turret2.y && turret1.r == turret2.r) {
            return -1;
        } else if (r3 > list.get(0) + list.get(1) ) {
            return 0;
        } else if (r3 == list.get(0) + list.get(1)) {
            return 1;
        } else if (r3 < list.get(0) + list.get(1)) {
            return 2;
        }
        return 0;
    }
}

 


해결

문제는 2개의 터렛의 탐지 범위에 있을 수 있는 마린의 개수를 구하는 것이다

뭔 터렛으로 지상유닛을 감지하고 있어

 

암튼 

두 원에서 겹치는 좌표를 구해야하는 것은 알겠는데

그것을 어떻게 구해야 하는지 인터넷에서 찾아보다가 잘못된 지식으로 코드를 작성하고 있었다

 

문제를 풀면서 이런식으로 반례를 찾아봤는데 대강 이해는하고있는데 잘안되었다

그러다 친구가 이건 삼각형을 구해야 한다고 너무 잘 설명해줘서 풀 수 있었다

 

 

728x90
반응형