본문 바로가기
PS 문제 풀이/Baekjoon

[BOJ] 백준 1002 터렛

by 까망 하르방 2021. 2. 21.
반응형

출처: https://www.acmicpc.net/problem/1002

 Input 

5

0 0 13 40 0 37

0 0 3 0 7 4

1 1 1 1 1 5

0 0 100 1 1 1

0 0 2 1 0 1

 

 Output 

2

1

0

0 (하나의 원안에 다른 원이 위치)

1 (내접)

아래와 같이 두 원의 중점과 반지름이 주어졌을 때, 교점 개수를 찾는 문제이다. 

두 점 사이의 거리 공식

 

두 원의 교점은 크게 6가지로 분류할 수 있다.

(1) 두 원이 2개의 지점에서 교차하는 경우

(2) 두 원이 1개의 지점에서 교차하는 경우

(3) 두 원이 만나지 않는 경우

(4) 두 원의 위치와 반지름(반경)이 동일하여 교차점이 무한대인 경우

(5) 한 개의 원이 다른 원안에 있는 경우

(6) 내접해서 한 개의 지점에서 교차하는 경우


import java.util.Scanner;

class Circle{
    private int x;
    private int y;
    private int r;
    
    public Circle(int x,int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }


    public int getY() {
        return y;
    }

    public int getR() {
        return r;
    }

    public void setR(int r) {
        this.r = r;
    }    
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int T = Integer.parseInt(sc.next());
        
        Circle[] circles1 = new Circle[T];
        Circle[] circles2 = new Circle[T];
        
        for(int i=0; i<T; i++) {
            int x1 = Integer.parseInt(sc.next());
            int y1 = Integer.parseInt(sc.next());
            int r1 = Integer.parseInt(sc.next());
            circles1[i] = new Circle(x1, y1);
            circles1[i].setR(r1);
            
            int x2 = Integer.parseInt(sc.next());
            int y2 = Integer.parseInt(sc.next());
            int r2 = Integer.parseInt(sc.next());
            circles2[i] = new Circle(x2, y2);
            circles2[i].setR(r2);
        }
        
        for(int i=0; i<T; i++) {
            
            if(circles2[i].getX() == circles1[i].getX() &&
                    circles2[i].getY() == circles1[i].getY() &&
                    circles2[i].getR() == circles1[i].getR()) {
                    
                    // 두 원이 완전 일치(원의 중심이 동일하며 반지름도 동일)
                    System.out.println("-1");
                
            }
            else {
                int dx = circles2[i].getX()-circles1[i].getX();
                int dy = circles2[i].getY()-circles1[i].getY();
                
                double d = Math.sqrt(dx*dx + dy*dy);
                double sumRadius = circles2[i].getR() + circles1[i].getR();
                
                if(d == sumRadius) { // 두 원이 한 군데에서 교차하는 경우
                    System.out.println("1");
                }else if(d < sumRadius){
                    if(circles2[i].getR() > d + circles1[i].getR()) {
                        // 원1이 원2에 내부에 위치한 경우
                        System.out.println("0");
                    }else if(circles1[i].getR() > d + circles2[i].getR()) {
                        // 원2가 원1에 내부에 위치한 경우
                        System.out.println("0");
                    }else if(circles2[i].getR() == d + circles1[i].getR()) {
                        // 원1이 원2의 내부에서 내접하고 있는 경우
                        System.out.println("1");
                    }else if(circles1[i].getR() == d + circles2[i].getR()) {
                        // 원2가 원1의 내부에서 내접하고 있는 경우
                        System.out.println("1");
                    }else { 
                        // 두 원이 두 군데에서 교차하는 경우
                        System.out.println("2");                        
                    }
                }else { 
                    // 두 원이 멀리 떨어져 있는 경우
                    System.out.println("0");
                }
            }
        }
    }
}

 

반응형

댓글