[Baekjoon / JAVA] Baekjoon Algorithm 1002 Turret
[Baekjoon / JAVA] Baekjoon Algorithm 1002 Turret
| Rank | Language Used |
|---|---|
🖼️ JAVA |
| Time Limit | Memory Limit |
|---|---|
| 2 sec | 128MB |
Kyuhyun Cho and Seunghwan Baek are employees working at the Turret. But their presence is so faint that they don't even count toward the population. Below are photos of Kyuhyun Cho and Seunghyun Baek.
Seokwon Lee ordered Kyuhyun Cho and Seunghwan Baek to calculate the location of the enemy marine (Jaeyoung Ryu). Kyuhyun Cho and Seunghwan Baek each calculated the distance from their own turret's position to the current enemy.
Given Kyuhyun Cho's coordinates and Seunghwan Baek's coordinates , along with the distance that Kyuhyun Cho calculated to Jaemyung Ryu and the distance that Seunghwan Baek calculated to Jaemyung Ryu, write a program that prints the number of possible coordinates where Jaemyung Ryu could be located.
The first line gives the number of test cases T. Each test case consists of the following.
One line gives , , , , , . , , , are points greater than or equal to -10,000 and less than or equal to 10,000, and , are natural numbers less than or equal to 10,000.
For each test case, print the number of possible positions where Jaemyung Ryu could be located. If the number of possible positions is infinite, print -1.
- Input
TC
3 0 0 13 40 0 37 0 0 30 0 7 4 1 1 1 1 1 5
- Output
TC
2 1 0
The example uses people's names for its elements, but since that gets in the way of understanding the problem, it can be simply restated as follows.
There are points , , at arbitrary positions, and we're given the distance between and , and the distance between and .
The task is to find the number of points where could actually be located. In other words, this can simply be defined as the problem of finding the intersection points of two circles.
There's a condition that if the circles overlap perfectly, since there are infinitely many such points, the answer should be expressed as -1.
This can be illustrated as follows.
The variables can be organized as follows.
| 1 | A's x-coordinate | A's y-coordinate | A's radius |
| 2 | B's x-coordinate | B's y-coordinate | B's radius |
The cases can be broken down into 6 total categories.
- The two circles overlap exactly (-1)
- The two circles overlap without touching (0)
- The two circles don't overlap and don't touch (0)
- The two circles overlap and are tangent (1)
- The two circles don't overlap but are tangent (1)
- The two circles overlap without touching (2)
This solution proceeds using the distance () between , and , , along with the sum () of and , and the absolute value of their difference ().
-
Case 1 - The two circles overlap exactly
This is the situation where the position and radius of both circles are identical.
This holds when is 0 and the lengths of and are equal. -
Case 2 - The two circles overlap without touching
This is the situation where the distance between the origins of the two circles is shorter than the difference of the radii.
This holds when . -
Case 3 - The two circles don't overlap and don't touch
This is the situation where the distance between the origins of the two circles is greater than the sum of the radii.
This holds when . -
Case 4 - The two circles overlap and are tangent
This is the situation where the distance between the origins of the two circles matches the difference of the radii.
This holds when . -
Case 5 - The two circles don't overlap but are tangent
This is the situation where the distance between the origins of the two circles matches the sum of the radii.
This holds when . -
Case 6 - The two circles overlap without touching
This is the situation where the two circles overlap each other to some moderate degree.
This holds when .
Even without expressing it as a formula, you can simply apply this to any situation that doesn't match the five cases above.
The above cases can be appropriately expressed using if statements. A switch statement determines branching based on a single variable, so it's somewhat unsuitable for applying to this algorithm.
JAVA
import java.util.Scanner; /** * Baekjoon Problem 1002 algorithm class * * @author RWB * @see <a href="https://blog.itcode.dev/posts/2021/05/21/a1002">1002 solution</a> * @since 2021.04.21 Wed 21:56:10 */ public class Main { /** * Main function * * @param args: [String[]] parameters */ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int length = scanner.nextInt(); for (int i = 0; i < length; i++) { int x1 = scanner.nextInt(); int y1 = scanner.nextInt(); int r1 = scanner.nextInt(); int x2 = scanner.nextInt(); int y2 = scanner.nextInt(); int r2 = scanner.nextInt(); System.out.println(calcPoints(x1, y1, r1, x2, y2, r2)); } } /** * Function that returns the number of intersection points * * case 1 - The two circles overlap exactly (-1) * case 2 - The two circles overlap without touching (0) * case 3 - The two circles don't overlap and don't touch (0) * case 4 - The two circles overlap and are tangent (1) * case 5 - The two circles don't overlap but are tangent (1) * case 6 - The two circles overlap without touching (2) * * @param x1: [int] A's x-coordinate * @param y1: [int] A's y-coordinate * @param r1: [int] distance between A and C * @param x2: [int] B's x-coordinate * @param y2: [int] B's y-coordinate * @param r2: [int] distance between B and C * * @return [int] number of intersection points */ private static int calcPoints(int x1, int y1, int r1, int x2, int y2, int r2) { // Formula to calculate the distance between two points double distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); int sum = r1 + r2; int sub = Math.abs(r1 - r2); // case 1 - The two circles overlap exactly if (distance == 0 && r1 == r2) { return -1; } // case 2 - The two circles overlap without touching else if (distance < sub) { return 0; } // case 3 - The two circles don't overlap and don't touch else if (distance > sum) { return 0; } // case 4 - The two circles overlap and are tangent else if (distance == sub) { return 1; } // case 5 - The two circles don't overlap but are tangent else if (distance == sum) { return 1; } // case 6 - The two circles overlap without touching else { return 2; } } }
- Math
- Geometry
